Spring 基于Aspectj切面表达式(6)

复制代码
 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 }
复制代码

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

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

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

原文地址:https://www.cnblogs.com/weiqingfeng/p/9497981.html