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语法基础-----切点表达 ...
随机推荐
- Go 算术运算符
Go 算术运算符 package main import "fmt" func main() { var a int = 21 var b int = 10 var c int c ...
- Photon Server与Unity3D客户端的交互
Photon Server与Unity3D的交互分为3篇博文实现 (1)Photon Server的服务器端配置 (2)Photon Server的Unity3D客户端配置 (3)Photon Ser ...
- 牛客多校第九场 D Knapsack Cryptosystem 背包
题意: 给你32个物品,给定一个容积,让你恰好把这个背包装满,求出装满的方案 题解: 暴力计算的话,复杂度$2^{32}$肯定会炸,考虑一种类似bsgs的算法,先用$2^{16}$的时间遍历前一半物品 ...
- 剑指offer——09青蛙跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 题解: 说俗一点,就是找规律: 不不,首先,我们分析一下,青蛙第一 ...
- webAPI(DOM) 2.1 获取页面元素 | 事件1 | 属性操作 | 节点 | 创建元素 | 事件2
js分三个部分: ECMAScript标准:js的基本语法 DOM:Ducument Object Model--->文档对象模型--->操作页面的元素 BOM:Browser Objec ...
- [Java]读取文件方法大全(转载)
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图 ...
- python读取Excel表格文件
python读取Excel表格文件,例如获取这个文件的数据 python读取Excel表格文件,需要如下步骤: 1.安装Excel读取数据的库-----xlrd 直接pip install xlrd安 ...
- POJ 3667 线段树区间合并裸题
题意:给一个n和m,表示n个房间,m次操作,操作类型有2种,一种把求连续未租出的房间数有d个的最小的最左边的房间号,另一个操作时把从x到x+d-1的房间号收回. 建立线段树,值为1表示未租出,0为租出 ...
- Response案例1_重定向
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.serv ...
- iOS开发系列-定时器强引用问题
概述 iOS开发中常用的定时器NSTimer.CADisplayLink. NSTimer 和 CADisplayLink 基本使用 NSTimer的创建方法有两个scheduledTimerWith ...