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 }

【注意】:在本类使用切面表单时,只需要引用方法名()即可
其它本包中的类:类名.方法()
其它非本包中的类:包名.类名.方法名()
Spring 基于Aspectj切面表达式(6)的更多相关文章
- Spring 基于Aspectj切面表达式
package com.proc; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; im ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探
由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...
- Spring基于AspectJ的AOP的开发——注解
源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...
- Spring AOP AspectJ Pointcut 表达式例子
主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- Spring基于AspectJ的AOP的开发之AOP的相关术语
1. Joinpoint(连接点) -- 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点(任何一个方法都可以称为连接点) 2. Pointc ...
- 基于@AspectJ配置Spring AOP之一--转
原文地址:http://tech.it168.com/j/2007-08-30/200708302209432.shtml 概述 在低版本Spring中定义一个切面是比较麻烦的,需要实现特定的接口,并 ...
- Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面
1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...
随机推荐
- mysql 删除同样记录只保留一条
delete from fa_order_account ) as a) ) as b)
- CF601C Kleofáš and the n-thlon(期望+前缀和优化dp)
传送门 解题思路 要求这个人的排名,我们可以先求出某个人比他排名靠前的概率,然后再乘上\(m-1\)即为答案.求某个人比他排名靠前可以用\(dp\),设\(f[i][j]\)表示前\(i\)场比赛某人 ...
- 虚拟IP---Linux下一个网卡配置多个IP
转:http://blog.csdn.net/turkeyzhou/article/details/16971225 Linux下配置网卡ip别名何谓ip别名?用windows的话说,就是为一个网卡配 ...
- 其它课程中的python---2、NumPy模块
其它课程中的python---2.NumPy模块 一.总结 一句话总结: numpy在数组计算方面又快又方便 1.NumPy中的ndarray是一个多维数组对象,该对象由哪两部分组成? -实际的数据 ...
- 把swf反编译成fla的几种方法
2007年著 第一种方法: 利用IMPERATOR FLA1.63 ,这个软件有演示版 和正式版 , 演示版不能反编译Action Scropt,在利用正式版反编译的过程中有时会丢失Action Sc ...
- uoj33 【UR #2】树上GCD
题目 大致是长剖+\(\rm dsu\ on\ tree\)的思想 先做一个转化,改为对于\(i\in[1,n-1]\)求出有多少个\(f(u,v)\)满足\(i|f(u,v)\),这样我们最后再做一 ...
- java 一维数组的输出方式
1.使用传统的for()循环输出: //定义一个数组 int []array = {1,2,3,4,5}; for(int i=0;i<array.length;i++) { System.ou ...
- 2018-8-10-win10-uwp-如何让一个集合按照需要的顺序进行排序
title author date CreateTime categories win10 uwp 如何让一个集合按照需要的顺序进行排序 lindexi 2018-08-10 19:16:50 +08 ...
- x25, PF_X25 - ITU-T X.25 / ISO-8208 协议接口。
总览 #include <sys/socket.h> #include <linux/x25.h> x25_socket = socket(PF_X25, SOCK_SEQPA ...
- override new 的区别
override : 方法提供从基类继承的成员的新实现. 通过 override 声明重写的方法称为重写基方法. 重写基方法必须具有与 override方法相同的签名 new : 关键字可以显式隐藏从 ...