Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
总结:
理解AOP@Before,@After,@AfterReturning,@AfterThrowing执行顺序
实现AOP的切面主要有以下几个要素:
使用@Aspect注解将一个java类定义为切面类
使用@Pointcut定义一个切入点,可以是一个规则表达式,比如下例中某个package下的所有函数,也可以是一个注解等。
根据需要在切入点不同位置的切入内容
使用@Before在切入点开始处切入内容
使用@After在切入点结尾处切入内容
使用@AfterReturning在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
使用@Around在切入点前后切入内容,并自己控制何时执行切入点自身的内容
使用@AfterThrowing用来处理当切入内容部分抛出异常之后的处理逻辑
try{
try{
doBefore();//对应@Before注解的方法切面逻辑
method.invoke();
}finally{
doAfter();//对应@After注解的方法切面逻辑
}
doAfterReturning();//对应@AfterReturning注解的方法切面逻辑
}catch(Exception e){
doAfterThrowing();//对应@AfterThrowing注解的方法切面逻辑
}
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before、@Around和@After等advice。最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了AOP功能。我使用到了@Before、@Around这两个advice。但在,使用过程中,却对它们的执行顺序并不清楚。为了弄清楚在不同情况下,这些advice到底是以怎么样的一个顺序进行执行的,我作了个测试,在此将其记录下来,以供以后查看。
前提
- 对于AOP相关类(aspect、pointcut等)的概念,本文不作说明。
- 对于如何让spring框架扫描到AOP,本文也不作说明。
情况一: 一个方法只被一个Aspect类拦截
当一个方法只被一个Aspect拦截时,这个Aspect中的不同advice是按照怎样的顺序进行执行的呢?请看:
添加 PointCut类
该pointcut用来拦截test包下的所有类中的所有方法。
package test;
import org.aspectj.lang.annotation.Pointcut; public class PointCuts { @Pointcut(value = "within(test.*)") public void aopDemo() { }
} package test; import org.aspectj.lang.annotation.Pointcut; public class PointCuts
{ @Pointcut(value = "within(test.*)") public void aopDemo() { } }
添加Aspect类
该类中的advice将会用到上面的pointcut,使用方法请看各个advice的value属性。
package test;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect public class Aspect1 { @Before(value = "test.PointCuts.aopDemo()") public void before(JoinPoint joinPoint) { System.out.println("[Aspect1] before advise"); } @Around(value = "test.PointCuts.aopDemo()") public void around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("[Aspect1] around advise 1"); pjp.proceed(); System.out.println("[Aspect1] around advise2"); } @AfterReturning(value = "test.PointCuts.aopDemo()") public void afterReturning(JoinPoint joinPoint) { System.out.println("[Aspect1] afterReturning advise"); } @AfterThrowing(value = "test.PointCuts.aopDemo()") public void afterThrowing(JoinPoint joinPoint) { System.out.println("[Aspect1] afterThrowing advise"); } @After(value = "test.PointCuts.aopDemo()") public void after(JoinPoint joinPoint) { System.out.println("[Aspect1] after advise");
} }
添加测试用Controller
添加一个用于测试的controller,这个controller中只有一个方法,但是它会根据参数值的不同,会作出不同的处理:一种是正常返回一个对象,一种是抛出异常(因为我们要测试@AfterThrowing这个advice)
package test;
import test.exception.TestException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping(value = "/aop") public class AopTestController { @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/test", method = RequestMethod.GET) public Result test(@RequestParam boolean throwException) { // case 1 if (throwException) { System.out.println("throw an exception"); throw new TestException("mock a server exception");
} // case 2 System.out.println("test OK"); return new Result() {{ this.setId(); this.setName("mock a Result"); }}; } public static class Result { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } }
测试 正常情况
在浏览器直接输入以下的URL,回车:
http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false
我们会看到输出的结果是:
[Aspect1] around advise [Aspect1] before advise test OK [Aspect1] around advise2 [Aspect1] after advise [Aspect1] afterReturning advise
测试 异常情况
在浏览器中直接输入以下的URL,回车:
http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true
我们会看到输出的结果是:
[Aspect1] around advise [Aspect1] before advise throw an exception [Aspect1] after advise [Aspect1] afterThrowing advise
结论
在一个方法只被一个aspect类拦截时,aspect类内部的 advice 将按照以下的顺序进行执行:
正常情况:
异常情况:
情况二: 同一个方法被多个Aspect类拦截
此处举例为被两个aspect类拦截。
有些情况下,对于两个不同的aspect类,不管它们的advice使用的是同一个pointcut,还是不同的pointcut,都有可能导致同一个方法被多个aspect类拦截。那么,在这种情况下,这多个Aspect类中的advice又是按照怎样的顺序进行执行的呢?请看:
pointcut类保持不变
添加一个新的aspect类
package test;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect public class Aspect2 { @Before(value = "test.PointCuts.aopDemo()") public void before(JoinPoint joinPoint) { System.out.println("[Aspect2] before advise"); } @Around(value = "test.PointCuts.aopDemo()") public void around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("[Aspect2] around advise 1"); pjp.proceed(); System.out.println("[Aspect2] around advise2"); } @AfterReturning(value = "test.PointCuts.aopDemo()") public void afterReturning(JoinPoint joinPoint) { System.out.println("[Aspect2] afterReturning advise"); } @AfterThrowing(value = "test.PointCuts.aopDemo()") public void afterThrowing(JoinPoint joinPoint) { System.out.println("[Aspect2] afterThrowing advise"); } @After(value = "test.PointCuts.aopDemo()") public void after(JoinPoint joinPoint) { System.out.println("[Aspect2] after advise"); }
}
测试用Controller也不变
还是使用上面的那个Controller。但是现在 aspect1 和 aspect2 都会拦截该controller中的方法。
下面继续进行测试!
测试 正常情况
在浏览器直接输入以下的URL,回车:
http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false
我们会看到输出的结果是:
[Aspect2] around advise [Aspect2] before advise [Aspect1] around advise [Aspect1] before advise test OK [Aspect1] around advise2 [Aspect1] after advise [Aspect1] afterReturning advise [Aspect2] around advise2 [Aspect2] after advise [Aspect2] afterReturning advise
但是这个时候,我不能下定论说 aspect2 肯定就比 aspect1 先执行。
不信?你把服务务器重新启动一下,再试试,说不定你就会看到如下的执行结果:
[Aspect1] around advise [Aspect1] before advise [Aspect2] around advise [Aspect2] before advise test OK [Aspect2] around advise2 [Aspect2] after advise [Aspect2] afterReturning advise [Aspect1] around advise2 [Aspect1] after advise [Aspect1] afterReturning advise
也就是说,这种情况下, aspect1 和 aspect2 的执行顺序是未知的。那怎么解决呢?不急,下面会给出解决方案。
测试 异常情况
在浏览器中直接输入以下的URL,回车:
http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true
我们会看到输出的结果是:
[Aspect2] around advise [Aspect2] before advise [Aspect1] around advise [Aspect1] before advise throw an exception [Aspect1] after advise [Aspect1] afterThrowing advise [Aspect2] after advise [Aspect2] afterThrowing advise
同样地,如果把服务器重启,然后再测试的话,就可能会看到如下的结果:
[Aspect1] around advise [Aspect1] before advise [Aspect2] around advise [Aspect2] before advise throw an exception [Aspect2] after advise [Aspect2] afterThrowing advise [Aspect1] after advise [Aspect1] afterThrowing advise
也就是说,同样地,异常情况下, aspect1 和 aspect2 的执行顺序也是未定的。
那么在 情况二 下,如何指定每个 aspect 的执行顺序呢?
方法有两种:
- 实现org.springframework.core.Ordered接口,实现它的getOrder()方法
- 给aspect添加@Order注解,该注解全称为:org.springframework.core.annotation.Order
不管采用上面的哪种方法,都是值越小的 aspect 越先执行。
比如,我们为 apsect1 和 aspect2 分别添加 @Order 注解,如下:
@Order()
@Component
@Aspect
public class Aspect1 {
// ...
}
@Order()
@Component
@Aspect
public class Aspect2 {
// ...
}
这样修改之后,可保证不管在任何情况下, aspect1 中的 advice 总是比 aspect2 中的 advice 先执行。如下图所示:
注意点
如果在同一个 aspect 类中,针对同一个 pointcut,定义了两个相同的 advice(比如,定义了两个 @Before),那么这两个 advice 的执行顺序是无法确定的,哪怕你给这两个 advice 添加了 @Order 这个注解,也不行。这点切记。
对于@Around这个advice,不管它有没有返回值,但是必须要方法内部,调用一下 pjp.proceed();否则,Controller 中的接口将没有机会被执行,从而也导致了 @Before这个advice不会被触发。比如,我们假设正常情况下,执行顺序为”aspect2 -> apsect1 -> controller”,如果,我们把 aspect1中的@Around中的 pjp.proceed();给删掉,那么,我们看到的输出结果将是:从结果可以发现, Controller 中的 接口 未被执行,aspect1 中的 @Before advice 也未被执行。
[Aspect2] around advise [Aspect2] before advise [Aspect1] around advise [Aspect1] around advise2 [Aspect1] after advise [Aspect1] afterReturning advise [Aspect2] around advise2 [Aspect2] after advise [Aspect2] afterReturning advise
参考:Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置的更多相关文章
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- Spring AOP 的@Aspect
Spring AOP 的@Aspect 转自:http://blog.csdn.net/tanghw/article/details/3862987 从Spring 2.0开始,可以使用基于sch ...
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring bean的介绍以及xml和注解的配置方法
5.Bean 下边我们来了解一下Bean的: Bean的作用域Bean的生命周期Bean的自动装配Resources和ResourceLoader 5.1Bean容器的初始化 Bean容器的初始化 两 ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring配置文件中bean标签中init-method和destroy-method和用注解方式配置
Person类: public class Person { private int i = 0; public Person(){ System.o ...
随机推荐
- 电脑如何用HDMI连接电视
因为现在的液晶电视基本上都有VGA接口,所以你可以很容易地用VGA线实现电脑连接电视上,但是该文有一个地方没有提到,那就是分辨率的问 题,现在的液晶电视的主流面板已经是全高清面板(1920X1080) ...
- ESP32:mdns协议
mdns(即多播dns,Multicast DNS)主要实现了在没有传统DNS服务器的情况下使局域网内的主机实现相互发现和通信,使用的端口为5353,遵从dns协议,使用现有的DNS信息结构.语法和资 ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 参数验证
示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 参数验证 参数验证功能是基于JSR303实现的,用户只需标识JSR303标准的验证Annotation,并通过声明filter来 ...
- JS中caller和callee
caller: caller是函数对象的一个属性,指的是这个函数对象的调用者,如果调用者,如果是顶层调用者,则返回null. 例: function func(){ console.log(func. ...
- glance系列一:glance基础
一 什么是glance glance即image service,是为虚拟机的创建提供镜像的服务 二 为何要有glance 我们基于openstack是构建基本的Iaas平台对外提供虚拟机,而虚拟机在 ...
- Java性能优化之使用NIO提升性能(Buffer和Channel)
在软件系统中,由于IO的速度要比内存慢,因此,I/O读写在很多场合都会成为系统的瓶颈.提升I/O速度,对提升系统整体性能有着很大的好处. 在Java的标准I/O中,提供了基于流的I/O实现,即Inpu ...
- 牛客---java练习
一. 1. abstract可以修饰方法和类,不能修饰属性.抽象方法没有方法体,即没有大括号{}.抽象类中的成员属性都是public static final类型的:成员方法都是public abst ...
- JS 作用域及作用域链
一.作用域 在 Javascript 中,作用域分为 全局作用域 和 函数作用域 全局作用域: 代码在程序的任何地方都能被访问,window 对象的内置属性都拥有全局作用域. 函数作用域: 在固定的代 ...
- Python编码与变量
(一)Python执行的方式 Window: 在CMD里面,使用 Python + 相对路径/绝对路径 在解释器里面,直接输入,一行代码一行代码的解释 Linux: 明确地指出用Python解释器来执 ...
- Java向下转型的意义
一开始学习 Java 时不重视向下转型.一直搞不清楚向下转型的意义和用途,不清楚其实就是不会,那开发的过程肯定也想不到用向下转型. 其实向上转型和向下转型都是很重要的,可能我们平时见向上转型多一点,向 ...