基于@Aspect的AOP配置

来源:互联网 发布:冰鉴 出版社 知乎 编辑:程序博客网 时间:2024/05/22 00:22

原文链接:http://luan.iteye.com/blog/1836246

1、Spring除了支持Schema方式配置AOP,还支持注解方式:使用@Aspect来配置

2、Spring默认不支持@Aspect风格的切面声明,通过如下配置开启@Aspect支持:

Java代码  收藏代码
  1. <aop:aspectj-autoproxy/>  

3、通过以上配置,Spring就能发现用@Aspect注解的切面内并把它应用到目标对象上。

4、定义一个切面:

Java代码  收藏代码
  1. @Aspect  
  2. public class AspectStyle {  
  3.   
  4.     @Before("execution(* com.sxit..*.*(..))")  
  5.     public void before(){  
  6.         System.out.println("方法执行前执行.....");  
  7.     }  
  8. }  

 5、后置返回通知:

Java代码  收藏代码
  1. @AfterReturning("execution(* com.sxit..*.*(..))")  
  2. public void afterReturning(){  
  3.         System.out.println("方法执行完执行.....");  
  4. }  

 6、后置异常通知:

Java代码  收藏代码
  1. @AfterThrowing("execution(* com.sxit..*.*(..))")  
  2. public void throwss(){  
  3.         System.out.println("方法异常时执行.....");  
  4. }  

 7、后置最终通知:

Java代码  收藏代码
  1. @After("execution(* com.sxit..*.*(..))")  
  2. public void after(){  
  3.         System.out.println("方法最后执行.....");  
  4. }  

 8、环绕通知:

Java代码  收藏代码
  1. @Around("execution(* com.sxit..*.*(..))")  
  2. public Object around(ProceedingJoinPoint pjp){  
  3.         System.out.println("方法环绕start.....");  
  4.         try {  
  5.             pjp.proceed();  
  6.         } catch (Throwable e) {  
  7.             e.printStackTrace();  
  8.         }  
  9.         System.out.println("方法环绕end.....");  
  10. }  

 9、按上面的每一个通知都要写一个定义,其实这部分可以抽出来,定义个一个公共的切入点。

Java代码  收藏代码
  1. package com.sxit;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11.   
  12. @Aspect  
  13. public class AspectStyle {  
  14.       
  15.     @Pointcut("execution(* com.sxit..*.*(..))")  
  16.     public void init(){  
  17.           
  18.     }  
  19.   
  20.     @Before(value="init()")  
  21.     public void before(){  
  22.         System.out.println("方法执行前执行.....");  
  23.     }  
  24.       
  25.     @AfterReturning(value="init()")  
  26.     public void afterReturning(){  
  27.         System.out.println("方法执行完执行.....");  
  28.     }  
  29.       
  30.     @AfterThrowing(value="init()")  
  31.     public void throwss(){  
  32.         System.out.println("方法异常时执行.....");  
  33.     }  
  34.       
  35.     @After(value="init()")  
  36.     public void after(){  
  37.         System.out.println("方法最后执行.....");  
  38.     }  
  39.       
  40.     @Around(value="init()")  
  41.     public Object around(ProceedingJoinPoint pjp){  
  42.         System.out.println("方法环绕start.....");  
  43.         Object o = null;  
  44.         try {  
  45.             o = pjp.proceed();  
  46.         } catch (Throwable e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.         System.out.println("方法环绕end.....");  
  50.         return o;  
  51.     }  
  52. }  

 10、打印信息:

Java代码  收藏代码
  1. 方法before前执行.....  
  2. 方法环绕start.....  
  3. 我看.....................  
  4. 方法after执行.....  
  5. 方法环绕end.....  
  6. 方法afterReurning执行.....  

 

参考自:http://jinnianshilongnian.iteye.com/blog/1418598

              http://ch-space.iteye.com/blog/493956



0 0
原创粉丝点击