你知道Spring AOP @Before @Around @After 的执行顺序吗

阿飞云 2019-06-10 23:16:38

做一个积极的人

编码、改bug、提升自己

我有一个乐园,面向编程,春暖花开!

你知道Spring AOP @Before @Around @After 的执行顺序吗

 

作者:rainbow702

原文地址:https://dwz.cn/uLMHZN5P

用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before@Around@After等advice。最近,为了实现项目中的输出日志权限控制这两个需求,我也使用到了AOP功能。我使用到了@Before@Around这两个advice。但在,使用过程中,却对它们的执行顺序并不清楚。为了弄清楚在不同情况下,这些advice到底是以怎么样的一个顺序进行执行的,我作了个测试,在此将其记录下来,以供以后查看。

前提

  • 对于AOP相关类(aspect、pointcut等)的概念,本文不作说明。
  • 对于如何让spring框架扫描到AOP,本文也不作说明。

情况一: 一个方法只被一个Aspect类拦截

当一个方法只被一个Aspect拦截时,这个Aspect中的不同advice是按照怎样的顺序进行执行的呢?请看:

添加 PointCut类

该pointcut用来拦截test包下的所有类中的所有方法。

 
  1. package test;

  2. import org.aspectj.lang.annotation.Pointcut;

  3. public class PointCuts {

  4. @Pointcut(value = "within(test.*)")

  5. public void aopDemo() {

  6. }

  7. }

添加Aspect类

该类中的advice将会用到上面的pointcut,使用方法请看各个advice的value属性。

 
  1. package test;

  2. import org.aspectj.lang.JoinPoint;

  3. import org.aspectj.lang.ProceedingJoinPoint;

  4. import org.aspectj.lang.annotation.*;

  5. import org.springframework.stereotype.Component;

  6. @Component

  7. @Aspect

  8. public class Aspect1 {

  9. @Before(value = "test.PointCuts.aopDemo()")

  10. public void before(JoinPoint joinPoint) {

  11. System.out.println("[Aspect1] before advise");

  12. }

  13. @Around(value = "test.PointCuts.aopDemo()")

  14. public void around(ProceedingJoinPoint pjp) throws Throwable{

  15. System.out.println("[Aspect1] around advise 1");

  16. pjp.proceed();

  17. System.out.println("[Aspect1] around advise2");

  18. }

  19. @AfterReturning(value = "test.PointCuts.aopDemo()")

  20. public void afterReturning(JoinPoint joinPoint) {

  21. System.out.println("[Aspect1] afterReturning advise");

  22. }

  23. @AfterThrowing(value = "test.PointCuts.aopDemo()")

  24. public void afterThrowing(JoinPoint joinPoint) {

  25. System.out.println("[Aspect1] afterThrowing advise");

  26. }

  27. @After(value = "test.PointCuts.aopDemo()")

  28. public void after(JoinPoint joinPoint) {

  29. System.out.println("[Aspect1] after advise");

  30. }

  31. }

添加测试用Controller

添加一个用于测试的controller,这个controller中只有一个方法,但是它会根据参数值的不同,会作出不同的处理:一种是正常返回一个对象,一种是抛出异常(因为我们要测试@AfterThrowing这个advice)

 
  1. package test;

  2. import test.exception.TestException;

  3. import org.springframework.http.HttpStatus;

  4. import org.springframework.web.bind.annotation.*;

  5. @RestController

  6. @RequestMapping(value = "/aop")

  7. public class AopTestController {

  8.  
  9. @ResponseStatus(HttpStatus.OK)

  10. @RequestMapping(value = "/test", method = RequestMethod.GET)

  11. public Result test(@RequestParam boolean throwException) {

  12. // case 1

  13. if (throwException) {

  14. System.out.println("throw an exception");

  15. throw new TestException("mock a server exception");

  16. }

  17. // case 2

  18. System.out.println("test OK");

  19. return new Result() {{

  20. this.setId(111);

  21. this.setName("mock a Result");

  22. }};

  23. }

  24.  
  25. public static class Result {

  26. private int id;

  27. private String name;

  28. public int getId() {

  29. return id;

  30. }

  31. public void setId(int id) {

  32. this.id = id;

  33. }

  34. public String getName() {

  35. return name;

  36. }

  37. public void setName(String name) {

  38. this.name = name;

  39. }

  40. }

  41. }

测试 正常情况

在浏览器直接输入以下的URL,回车:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false

我们会看到输出的结果是:

 
  1. [Aspect1] around advise 1

  2. [Aspect1] before advise

  3. test OK

  4. [Aspect1] around advise2

  5. [Aspect1] after advise

  6. [Aspect1] afterReturning advise

测试 异常情况

在浏览器中直接输入以下的URL,回车:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true

我们会看到输出的结果是:

 
  1. [Aspect1] around advise 1

  2. [Aspect1] before advise

  3. throw an exception

  4. [Aspect1] after advise

  5. [Aspect1] afterThrowing advise

结论

在一个方法只被一个aspect类拦截时,aspect类内部的 advice 将按照以下的顺序进行执行:

正常情况:

你知道Spring AOP @Before @Around @After 的执行顺序吗

 

异常情况:

你知道Spring AOP @Before @Around @After 的执行顺序吗

 

情况二: 同一个方法被多个Aspect类拦截

此处举例为被两个aspect类拦截。 有些情况下,对于两个不同的aspect类,不管它们的advice使用的是同一个pointcut,还是不同的pointcut,都有可能导致同一个方法被多个aspect类拦截。那么,在这种情况下,这多个Aspect类中的advice又是按照怎样的顺序进行执行的呢?请看:

pointcut类保持不变

添加一个新的aspect类

 
  1. package test;

  2. import org.aspectj.lang.JoinPoint;

  3. import org.aspectj.lang.ProceedingJoinPoint;

  4. import org.aspectj.lang.annotation.*;

  5. import org.springframework.stereotype.Component;

  6.  
  7. @Component

  8. @Aspect

  9. public class Aspect2 {

  10. @Before(value = "test.PointCuts.aopDemo()")

  11. public void before(JoinPoint joinPoint) {

  12. System.out.println("[Aspect2] before advise");

  13. }

  14. @Around(value = "test.PointCuts.aopDemo()")

  15. public void around(ProceedingJoinPoint pjp) throws Throwable{

  16. System.out.println("[Aspect2] around advise 1");

  17. pjp.proceed();

  18. System.out.println("[Aspect2] around advise2");

  19. }

  20. @AfterReturning(value = "test.PointCuts.aopDemo()")

  21. public void afterReturning(JoinPoint joinPoint) {

  22. System.out.println("[Aspect2] afterReturning advise");

  23. }

  24. @AfterThrowing(value = "test.PointCuts.aopDemo()")

  25. public void afterThrowing(JoinPoint joinPoint) {

  26. System.out.println("[Aspect2] afterThrowing advise");

  27. }

  28. @After(value = "test.PointCuts.aopDemo()")

  29. public void after(JoinPoint joinPoint) {

  30. System.out.println("[Aspect2] after advise");

  31. }

  32. }

测试用Controller也不变

还是使用上面的那个Controller。但是现在 aspect1 和 aspect2 都会拦截该controller中的方法。

下面继续进行测试!

测试 正常情况

在浏览器直接输入以下的URL,回车:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=false

我们会看到输出的结果是:

 
  1. [Aspect2] around advise 1

  2. [Aspect2] before advise

  3. [Aspect1] around advise 1

  4. [Aspect1] before advise

  5. test OK

  6. [Aspect1] around advise2

  7. [Aspect1] after advise

  8. [Aspect1] afterReturning advise

  9. [Aspect2] around advise2

  10. [Aspect2] after advise

  11. [Aspect2] afterReturning advise

但是这个时候,我不能下定论说 aspect2 肯定就比 aspect1 先执行。 不信?你把服务务器重新启动一下,再试试,说不定你就会看到如下的执行结果:

 
  1. [Aspect1] around advise 1

  2. [Aspect1] before advise

  3. [Aspect2] around advise 1

  4. [Aspect2] before advise

  5. test OK

  6. [Aspect2] around advise2

  7. [Aspect2] after advise

  8. [Aspect2] afterReturning advise

  9. [Aspect1] around advise2

  10. [Aspect1] after advise

  11. [Aspect1] afterReturning advise

也就是说,这种情况下, aspect1 和 aspect2 的执行顺序是未知的。那怎么解决呢?不急,下面会给出解决方案。

测试 异常情况

在浏览器中直接输入以下的URL,回车:

http://192.168.142.8:7070/aoptest/v1/aop/test?throwException=true

我们会看到输出的结果是:

 
  1. [Aspect2] around advise 1

  2. [Aspect2] before advise

  3. [Aspect1] around advise 1

  4. [Aspect1] before advise

  5. throw an exception

  6. [Aspect1] after advise

  7. [Aspect1] afterThrowing advise

  8. [Aspect2] after advise

  9. [Aspect2] afterThrowing advise

同样地,如果把服务器重启,然后再测试的话,就可能会看到如下的结果:

 
  1. [Aspect1] around advise 1

  2. [Aspect1] before advise

  3. [Aspect2] around advise 1

  4. [Aspect2] before advise

  5. throw an exception

  6. [Aspect2] after advise

  7. [Aspect2] afterThrowing advise

  8. [Aspect1] after advise

  9. [Aspect1] afterThrowing advise

也就是说,同样地,异常情况下, aspect1 和 aspect2 的执行顺序也是未定的。

那么在 情况二 下,如何指定每个 aspect 的执行顺序呢? 方法有两种:

  • 实现org.springframework.core.Ordered接口,实现它的getOrder()方法
  • 给aspect添加@Order注解,该注解全称为:org.springframework.core.annotation.Order

不管采用上面的哪种方法,都是值越小的 aspect 越先执行。 比如,我们为 apsect1 和 aspect2 分别添加 @Order 注解,如下:

 
  1. @Order(5)

  2. @Component

  3. @Aspect

  4. public class Aspect1 { }

  5. @Order(6)

  6. @Component

  7. @Aspect

  8. public class Aspect2 { }

这样修改之后,可保证不管在任何情况下, aspect1 中的 advice 总是比 aspect2 中的 advice 先执行。如下图所示:

你知道Spring AOP @Before @Around @After 的执行顺序吗

 

注意点

  • 如果在同一个 aspect 类中,针对同一个 pointcut,定义了两个相同的 advice(比如,定义了两个 @Before),那么这两个 advice 的执行顺序是无法确定的,哪怕你给这两个 advice 添加了 @Order 这个注解,也不行。这点切记。
  • 对于@Around这个advice,不管它有没有返回值,但是在方法内部,必须要调用一下 pjp.proceed(); 否则,Controller 中的接口将没有机会被执行,从而也导致了 @Before这个advice不会被触发。比如,我们假设正常情况下,执行顺序为”aspect2 -> apsect1 -> controller”,如果,我们把 aspect1中的@Around中的 pjp.proceed();给删掉,那么,我们看到的输出结果将是:
 
  1. [Aspect2] around advise 1

  2. [Aspect2] before advise

  3. [Aspect1] around advise 1

  4. [Aspect1] around advise2

  5. [Aspect1] after advise

  6. [Aspect1] afterReturning advise

  7. [Aspect2] around advise2

  8. [Aspect2] after advise

  9. [Aspect2] afterReturning advise

从结果可以发现, Controller 中的 接口 未被执行,aspect1 中的 @Before advice 也未被执行。

参考资料

Spring 4.3.2.RELEASE 官方资料:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/

其中,AOP的执行顺序章节为:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#aop-ataspectj-advice-ordering

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值