Spring的注解的AOP的通知类型
-
@Before:前置通知
-
@AfterReturning:后置通知
-
@Around:环绕通知
-
@AfterThrowing:异常抛出通知
-
@After:最终通知
-
@Pointcut:切入点的注解
1 /** 2 * 切面类:注解的切面类 3 / 4 @Aspect 5 public class MyAspectAnno { 6 //前置通知 7 @Before(value="execution( com.itheima.spring.demo1.OrderDao.save(..) )") 8 public void before(){ 9 System.out.println("前置通知======"); 10 } 11 //后置通知 12 @AfterReturning(value="execution(* com.itheima.spring.demo1.OrderDao.delete(..))", returning="result") 13 public void afterReturning(Object result){ 14 System.out.println("后置通知====="+result); 15 } 16 @Around(value="execution(* com.itheima.spring.demo1.OrderDao.update(..))") 17 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ 18 System.out.println("环绕前增强====="); 19 Object obj = joinPoint.proceed(); 20 System.out.println("环绕后增强====="); 21 return obj; 22 } 23 //异常抛出通知 24 @AfterThrowing(value="execution(* com.itheima.spring.demo1.OrderDao.find(..))" , throwing="e") 25 public void find(Throwable e ){ 26 System.out.println("异常抛出通知======"+e.getMessage()); 27 } 28 //最终通知: 29 @After(value="execution(* com.itheima.spring.demo1.OrderDao.find(..))") 30 public void after( ){ 31 System.out.println("最终通知======"); 32 } 33 }

切入点的注解:
配置@Pointcut注解,使用类名.方法
1/** 2 * 切面类:注解的切面类 3 */ 4@Aspect 5public class MyAspectAnno { 6 //切入点的注解 7 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.find(..))") 8 private void pointcut1(){} 9 10 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.save(..))") 11 private void pointcut2(){} 12 13 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.update(..))") 14 private void pointcut3(){} 15 16 @Pointcut(value="execution(* com.itheima.spring.demo1.OrderDao.delete(..))") 17 private void pointcut4(){} 18 19 //前置通知 20 @Before(value="MyAspectAnno.pointcut2()") 21 public void before(){ 22 System.out.println("前置通知======"); 23 } 24 //后置通知 25 @AfterReturning(value="MyAspectAnno.pointcut4()", returning="result") 26 public void afterReturning(Object result){ 27 System.out.println("后置通知====="+result); 28 } //环绕通知 29 @Around(value="MyAspectAnno.pointcut3()") 30 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ 31 System.out.println("环绕前增强====="); 32 Object obj = joinPoint.proceed(); 33 System.out.println("环绕后增强====="); 34 return obj; 35 } 36 //异常抛出通知 37 @AfterThrowing(value="MyAspectAnno.pointcut1()" , throwing="e") 38 public void find(Throwable e ){ 39 System.out.println("异常抛出通知======"+e.getMessage()); 40 } 41 42 // 最终通知: 43 @After(value="MyAspectAnno.pointcut1()") 44 public void after( ){ 45 System.out.println("最终通知======"); 46 } 47 48 49 50}