Spring3.0中的AOP注解配置

来源:互联网 发布:淘宝上几千的紫砂壶 编辑:程序博客网 时间:2024/04/28 00:56

转自:http://zywang.iteye.com/blog/974226

使用@AspectJ标签

  1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

示例:

Java代码  收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2. import org.aspectj.lang.annotation.After;  
  3. import org.aspectj.lang.annotation.AfterThrowing;  
  4. import org.aspectj.lang.annotation.Around;  
  5. import org.aspectj.lang.annotation.Aspect;  
  6. import org.aspectj.lang.annotation.Before;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. /** 
  10.  * 基于注解的AOP日志示例 
  11.  * @author ZYWANG 2011-3-24 
  12.  */  
  13. @Component  
  14. @Aspect  
  15. public class AopLog {  
  16.       
  17.     //方法执行前调用  
  18.     @Before("execution (* com.zywang.services.impl.*.*(..))")  
  19.     public void before() {  
  20.         System.out.println("before");  
  21.     }  
  22.       
  23.     //方法执行后调用  
  24.     @After("execution (* com.zywang.services.impl.*.*(..))")  
  25.     public void after() {  
  26.         System.out.println("after");  
  27.     }  
  28.       
  29.     //方法执行的前后调用  
  30.     @Around("execution (* com.zywang.services.impl.*.*(..))")  
  31.     public Object around(ProceedingJoinPoint point) throws Throwable{  
  32.         System.out.println("begin around");  
  33.         Object object = point.proceed();  
  34.         System.out.println("end around");  
  35.         return object;  
  36.     }  
  37.       
  38.     //方法运行出现异常时调用  
  39.     @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")  
  40.     public void afterThrowing(Exception ex){  
  41.         System.out.println("afterThrowing");  
  42.         System.out.println(ex);  
  43.     }  
  44. }  

上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

Java代码  收藏代码
  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2. import org.aspectj.lang.annotation.Around;  
  3. import org.aspectj.lang.annotation.Aspect;  
  4. import org.aspectj.lang.annotation.Before;  
  5. import org.aspectj.lang.annotation.Pointcut;  
  6. import org.springframework.stereotype.Component;  
  7.   
  8. /** 
  9.  * 基于注解的AOP日志示例 
  10.  * @author ZYWANG 2011-3-24 
  11.  */  
  12. @Component  
  13. @Aspect  
  14. public class AopLog {  
  15.       
  16.     @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")  
  17.     public void pointcut(){}  
  18.       
  19.     //方法执行前调用  
  20.     @Before("pointcut()")  
  21.     public void before() {  
  22.         System.out.println("before");  
  23.     }  
  24.       
  25.     //方法执行的前后调用  
  26.     @Around("pointcut()")  
  27.     public Object around(ProceedingJoinPoint point) throws Throwable{  
  28.         System.out.println("begin around");  
  29.         Object object = point.proceed();  
  30.         System.out.println("end around");  
  31.         return object;  
  32.     }  
  33. }