天天看点

Spring3.0中的AOP配置方法

第一种配置方法:使用@AspectJ标签

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

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

示例:

Java代码   

Spring3.0中的AOP配置方法
  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. @Component  
  9. @Aspect  
  10. public class AopLog {  
  11.     //方法执行前调用  
  12.     @Before("execution (* com.services.impl.*.*(..))")  
  13.     public void before() {  
  14.         System.out.println("before");  
  15.     }  
  16.     //方法执行后调用  
  17.     @After("execution (* com.services.impl.*.*(..))")  
  18.     public void after() {  
  19.         System.out.println("after");  
  20.     }  
  21.     //方法执行的前后调用  
  22.     @Around("execution (* com.services.impl.*.*(..))")  
  23.     public Object around(ProceedingJoinPoint point) throws Throwable{  
  24.         System.out.println("begin around");  
  25.         Object object = point.proceed();  
  26.         System.out.println("end around");  
  27.         return object;  
  28.     }  
  29.     //方法运行出现异常时调用  
  30.     @AfterThrowing(pointcut = "execution (* com.services.impl.*.*(..))",throwing = "ex")  
  31.     public void afterThrowing(Exception ex){  
  32.         System.out.println("afterThrowing");  
  33.         System.out.println(ex);  
  34.     }  
  35. }  

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

Java代码   

Spring3.0中的AOP配置方法
  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. @Component  
  8. @Aspect  
  9. public class AopLog {  
  10.     @Pointcut("execution (* com.services.impl.*.*(..))")  
  11.     public void pointcut(){}  
  12.     //方法执行前调用  
  13.     @Before("pointcut()")  
  14.     public void before() {  
  15.         System.out.println("before");  
  16.     }  
  17.     //方法执行的前后调用  
  18.     @Around("pointcut()")  
  19.     public Object around(ProceedingJoinPoint point) throws Throwable{  
  20.         System.out.println("begin around");  
  21.         Object object = point.proceed();  
  22.         System.out.println("end around");  
  23.         return object;  
  24.     }  
  25. }  

第二种配置方法:基于配置文件的配置

  1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
  2. 在Spring配置文件中注册该Java类为一个Bean
  3. 使用<aop:config/>、<aop:aspect/>等标签进行配置

示例:

Java文件

Java代码   

Spring3.0中的AOP配置方法
  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2. public class AopLog {  
  3.     //方法执行的前后调用  
  4.     public Object runOnAround(ProceedingJoinPoint point) throws Throwable{  
  5.         System.out.println("begin around");  
  6.         Object object = point.proceed();  
  7.         System.out.println("end around");  
  8.         return object;  
  9.     }  
  10. }  

 Spring配置文件

Xml代码   

Spring3.0中的AOP配置方法
  1. <bean id="aopLog" class="com.aop.AopLog"></bean>  
  2. <aop:config>  
  3.     <aop:aspect ref="aopLog">  
  4.         <aop:around method="runOnAround" pointcut="execution (* com.services.impl.*.*(..))"/>  
  5.     </aop:aspect>  
  6. </aop:config>  

 注意:上面这个示例使用的是around方式的拦截,该方法要求Java类中的方法有一个ProceedingJoinPoint类型的参数

使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示

以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》

最后加一句:spring3的AOP注释支持&&,||,!等符号,很方便,例如:

@Before("execution(* com.web.dwr..*.*(..)) && !execution(* com.web.dwr.BaseDwr.*(..))")