码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-08-18 18:31:26      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:RoCE   turn   cee   gif   work   tsig   package   asp   return   

技术分享图片
 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 }
技术分享图片

 

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

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

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

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

标签:RoCE   turn   cee   gif   work   tsig   package   asp   return   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!