天天看點

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.*(..))")