Spring 基於Aspectj切面表達式


 1 package com.proc;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.After;
 6 import org.aspectj.lang.annotation.AfterReturning;
 7 import org.aspectj.lang.annotation.AfterThrowing;
 8 import org.aspectj.lang.annotation.Around;
 9 import org.aspectj.lang.annotation.Aspect;
10 import org.aspectj.lang.annotation.Before;
11 import org.springframework.stereotype.Component;
12 
13 @Aspect
14 @Component
15 public class LoggingAspect {
16 
17     @Before("execution(* *.*(int,int))")
18     public void beforeMethod(JoinPoint point){
19         System.out.println("正在執行方法: "+point.getSignature().getName());
20     }
21     
22     @After("execution(* *.*(int,int))")
23     public void afterMethod(JoinPoint point){
24         System.out.println("方法執行結束: "+point.getSignature().getName());
25     }
26     
27     @AfterReturning(value="execution(* *.*(int,int))",returning="retVal")
28     public void afterReturningMethod(JoinPoint point,Object retVal){
29         System.out.println("方法: "+point.getSignature().getName()+"執行結果為:"+retVal);
30     }
31     
32     @AfterThrowing(value="execution(* *.*(int,int))",throwing="ex")
33     public void afterThrowingMethod(JoinPoint point,Exception ex){
34         System.out.println("執行方法: "+point.getSignature().getName()+"出現了異常:"+ex.getMessage());
35     }
36     
37     @Around("execution(* *.*(int,int))")
38     public Object aroundMethod(ProceedingJoinPoint point){
39         
40         System.out.println("環繞通知: "+point.getSignature().getName());
41         Object result=null;
42         //這里相當於前置通知
43         try {
44             //執行方法
45             result= point.proceed();
46             //這里相當於結果通知
47         } catch (Throwable e) {
48             //這里相當於異常通知
49             e.printStackTrace();
50             
51         }
52         //這里相當於后置通知
53         System.out.println("環繞通知: "+point.getSignature().getName());
54         return result;
55     }
56 }

 

在對應通知的表單時總要指定execution(* *.*(int,int)),修改也必將麻煩。為了方便我們引入了切面表單時@PointCut。

下面我們來看修改該后的代碼

 1 package com.proc;
 2 
 3 import org.aspectj.lang.JoinPoint;
 4 import org.aspectj.lang.ProceedingJoinPoint;
 5 import org.aspectj.lang.annotation.After;
 6 import org.aspectj.lang.annotation.AfterReturning;
 7 import org.aspectj.lang.annotation.AfterThrowing;
 8 import org.aspectj.lang.annotation.Around;
 9 import org.aspectj.lang.annotation.Aspect;
10 import org.aspectj.lang.annotation.Before;
11 import org.aspectj.lang.annotation.Pointcut;
12 import org.springframework.stereotype.Component;
13 
14 @Aspect
15 @Component
16 public class LoggingAspect {
17 
18     /**定義一個方法,用於聲明切面表達式,該方法中什么也不需要。使用是只需要引用該方法名即可*/
19     @Pointcut("execution(* *.*(..))")
20     public void declareJoinPointExpression(){}
21     
22     @Before("declareJoinPointExpression()")
23     public void beforeMethod(JoinPoint point){
24         System.out.println("正在執行方法: "+point.getSignature().getName());
25     }
26     
27     @After("declareJoinPointExpression()")
28     public void afterMethod(JoinPoint point){
29         System.out.println("方法執行結束: "+point.getSignature().getName());
30     }
31     
32     @AfterReturning(value="declareJoinPointExpression()",returning="retVal")
33     public void afterReturningMethod(JoinPoint point,Object retVal){
34         System.out.println("方法: "+point.getSignature().getName()+"執行結果為:"+retVal);
35     }
36     
37     @AfterThrowing(value="declareJoinPointExpression()",throwing="ex")
38     public void afterThrowingMethod(JoinPoint point,Exception ex){
39         System.out.println("執行方法: "+point.getSignature().getName()+"出現了異常:"+ex.getMessage());
40     }
41     
42     @Around("declareJoinPointExpression()")
43     public Object aroundMethod(ProceedingJoinPoint point){
44         
45         System.out.println("環繞通知: "+point.getSignature().getName());
46         Object result=null;
47         //這里相當於前置通知
48         try {
49             //執行方法
50             result= point.proceed();
51             //這里相當於結果通知
52         } catch (Throwable e) {
53             //這里相當於異常通知
54             e.printStackTrace();
55             
56         }
57         //這里相當於后置通知
58         System.out.println("環繞通知: "+point.getSignature().getName());
59         return result;
60     }
61 }

 

【注意】:在本類使用切面表單時,只需要引用方法名()即可

      其它本包中的類:類名.方法()

      其它非本包中的類:包名.類名.方法名()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM