基於@Aspect的AOP配置

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執行.....  
發佈了6 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章