总结:

理解AOP@Before,@After,@AfterReturning,@AfterThrowing执行顺序

实现AOP的切面主要有以下几个要素:

使用@Aspect注解将一个java类定义为切面类

使用@Pointcut定义一个切入点,可以是一个规则表达式,比如下例中某个package下的所有函数,也可以是一个注解等。

根据需要在切入点不同位置的切入内容

使用@Before在切入点开始处切入内容

使用@After在切入点结尾处切入内容

使用@AfterReturning在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)

使用@Around在切入点前后切入内容,并自己控制何时执行切入点自身的内容

使用@AfterThrowing用来处理当切入内容部分抛出异常之后的处理逻辑

  1. try{
  2. try{
  3. doBefore();//对应@Before注解的方法切面逻辑
  4. method.invoke();
  5. }finally{
  6. doAfter();//对应@After注解的方法切面逻辑
  7. }
  8. doAfterReturning();//对应@AfterReturning注解的方法切面逻辑
  9. }catch(Exception e){
  10. doAfterThrowing();//对应@AfterThrowing注解的方法切面逻辑
  11. }

用过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.  
  4. public class PointCuts {
  5.  
  6. @Pointcut(value = "within(test.*)")
  7.  
  8. public void aopDemo() {
  9.  
  10. }
  11. }
  12.  
  13. package test; import org.aspectj.lang.annotation.Pointcut;
  14.  
  15. public class PointCuts
  16. { @Pointcut(value = "within(test.*)") public void aopDemo() { } }

添加Aspect类

 

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

  1. package test;
  2. import org.aspectj.lang.JoinPoint;
  3.  
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5.  
  6. import org.aspectj.lang.annotation.*;
  7.  
  8. import org.springframework.stereotype.Component;
  9.  
  10. @Component
  11.  
  12. @Aspect
  13.  
  14. public class Aspect1 {
  15.  
  16. @Before(value = "test.PointCuts.aopDemo()")
  17.  
  18. public void before(JoinPoint joinPoint) {
  19.  
  20. System.out.println("[Aspect1] before advise");
  21.  
  22. }
  23.  
  24. @Around(value = "test.PointCuts.aopDemo()")
  25.  
  26. public void around(ProceedingJoinPoint pjp) throws Throwable{
  27.  
  28. System.out.println("[Aspect1] around advise 1");
  29.  
  30. pjp.proceed();
  31.  
  32. System.out.println("[Aspect1] around advise2");
  33.  
  34. }
  35.  
  36. @AfterReturning(value = "test.PointCuts.aopDemo()")
  37.  
  38. public void afterReturning(JoinPoint joinPoint) {
  39.  
  40. System.out.println("[Aspect1] afterReturning advise");
  41.  
  42. }
  43.  
  44. @AfterThrowing(value = "test.PointCuts.aopDemo()")
  45.  
  46. public void afterThrowing(JoinPoint joinPoint) {
  47.  
  48. System.out.println("[Aspect1] afterThrowing advise");
  49.  
  50. }
  51.  
  52. @After(value = "test.PointCuts.aopDemo()")
  53.  
  54. public void after(JoinPoint joinPoint) {
  55.  
  56. System.out.println("[Aspect1] after advise");
  57. }
  58.  
  59. }

添加测试用Controller

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

  1. package test;
  2. import test.exception.TestException;
  3.  
  4. import org.springframework.http.HttpStatus;
  5.  
  6. import org.springframework.web.bind.annotation.*;
  7.  
  8. @RestController
  9.  
  10. @RequestMapping(value = "/aop")
  11.  
  12. public class AopTestController {
  13.  
  14. @ResponseStatus(HttpStatus.OK)
  15.  
  16. @RequestMapping(value = "/test", method = RequestMethod.GET)
  17.  
  18. public Result test(@RequestParam boolean throwException) {
  19.  
  20. // case 1
  21.  
  22. if (throwException) {
  23.  
  24. System.out.println("throw an exception");
  25.  
  26. throw new TestException("mock a server exception");
  27. }
  28.  
  29. // case 2
  30.  
  31. System.out.println("test OK");
  32.  
  33. return new Result() {{
  34.  
  35. this.setId();
  36.  
  37. this.setName("mock a Result");
  38.  
  39. }};
  40.  
  41. }
  42.  
  43. public static class Result {
  44.  
  45. private int id;
  46.  
  47. private String name;
  48.  
  49. public int getId() {
  50.  
  51. return id;
  52.  
  53. }
  54.  
  55. public void setId(int id) {
  56.  
  57. this.id = id;
  58.  
  59. }
  60.  
  61. public String getName() {
  62.  
  63. return name;
  64.  
  65. }
  66.  
  67. public void setName(String name) {
  68.  
  69. this.name = name;
  70.  
  71. }
  72.  
  73. }
  74.  
  75. }

测试 正常情况

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

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

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

  1. [Aspect1] around advise
  2.  
  3. [Aspect1] before advise
  4.  
  5. test OK
  6.  
  7. [Aspect1] around advise2
  8.  
  9. [Aspect1] after advise
  10.  
  11. [Aspect1] afterReturning advise

测试 异常情况

 

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

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

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

  1. [Aspect1] around advise
  2.  
  3. [Aspect1] before advise
  4.  
  5. throw an exception
  6.  
  7. [Aspect1] after advise
  8.  
  9. [Aspect1] afterThrowing advise

结论

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

正常情况: 


异常情况: 

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

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

pointcut类保持不变

添加一个新的aspect类

  1. package test;
  2. import org.aspectj.lang.JoinPoint;
  3.  
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5.  
  6. import org.aspectj.lang.annotation.*;
  7.  
  8. import org.springframework.stereotype.Component;
  9.  
  10. @Component
  11.  
  12. @Aspect
  13.  
  14. public class Aspect2 {
  15.  
  16. @Before(value = "test.PointCuts.aopDemo()")
  17.  
  18. public void before(JoinPoint joinPoint) {
  19.  
  20. System.out.println("[Aspect2] before advise");
  21.  
  22. }
  23.  
  24. @Around(value = "test.PointCuts.aopDemo()")
  25.  
  26. public void around(ProceedingJoinPoint pjp) throws Throwable{
  27.  
  28. System.out.println("[Aspect2] around advise 1");
  29.  
  30. pjp.proceed();
  31.  
  32. System.out.println("[Aspect2] around advise2");
  33.  
  34. }
  35.  
  36. @AfterReturning(value = "test.PointCuts.aopDemo()")
  37.  
  38. public void afterReturning(JoinPoint joinPoint) {
  39.  
  40. System.out.println("[Aspect2] afterReturning advise");
  41.  
  42. }
  43.  
  44. @AfterThrowing(value = "test.PointCuts.aopDemo()")
  45.  
  46. public void afterThrowing(JoinPoint joinPoint) {
  47.  
  48. System.out.println("[Aspect2] afterThrowing advise");
  49.  
  50. }
  51.  
  52. @After(value = "test.PointCuts.aopDemo()")
  53.  
  54. public void after(JoinPoint joinPoint) {
  55.  
  56. System.out.println("[Aspect2] after advise");
  57.  
  58. }
  59. }

测试用Controller也不变

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

下面继续进行测试!

测试 正常情况

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

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

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

  1. [Aspect2] around advise
  2.  
  3. [Aspect2] before advise
  4.  
  5. [Aspect1] around advise
  6.  
  7. [Aspect1] before advise
  8.  
  9. test OK
  10.  
  11. [Aspect1] around advise2
  12.  
  13. [Aspect1] after advise
  14.  
  15. [Aspect1] afterReturning advise
  16.  
  17. [Aspect2] around advise2
  18.  
  19. [Aspect2] after advise
  20.  
  21. [Aspect2] afterReturning advise

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

  1. [Aspect1] around advise
  2.  
  3. [Aspect1] before advise
  4.  
  5. [Aspect2] around advise
  6.  
  7. [Aspect2] before advise
  8.  
  9. test OK
  10.  
  11. [Aspect2] around advise2
  12.  
  13. [Aspect2] after advise
  14.  
  15. [Aspect2] afterReturning advise
  16.  
  17. [Aspect1] around advise2
  18.  
  19. [Aspect1] after advise
  20.  
  21. [Aspect1] afterReturning advise

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

测试 异常情况

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

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

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

  1. [Aspect2] around advise
  2.  
  3. [Aspect2] before advise
  4.  
  5. [Aspect1] around advise
  6.  
  7. [Aspect1] before advise
  8.  
  9. throw an exception
  10.  
  11. [Aspect1] after advise
  12.  
  13. [Aspect1] afterThrowing advise
  14.  
  15. [Aspect2] after advise
  16.  
  17. [Aspect2] afterThrowing advise

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

  1. [Aspect1] around advise
  2.  
  3. [Aspect1] before advise
  4.  
  5. [Aspect2] around advise
  6.  
  7. [Aspect2] before advise
  8.  
  9. throw an exception
  10.  
  11. [Aspect2] after advise
  12.  
  13. [Aspect2] afterThrowing advise
  14.  
  15. [Aspect1] after advise
  16.  
  17. [Aspect1] afterThrowing advise

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

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

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

不管采用上面的哪种方法,都是值越小的 aspect 越先执行。

比如,我们为 apsect1 和 aspect2 分别添加 @Order 注解,如下:

  1. @Order()
  2.  
  3. @Component
  4.  
  5. @Aspect
  6.  
  7. public class Aspect1 {
  8.  
  9. // ...
  10.  
  11. }
  12. @Order()
  13.  
  14. @Component
  15. @Aspect
  16. public class Aspect2 {
  17. // ...
  18.  
  19. }

这样修改之后,可保证不管在任何情况下, 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 也未被执行。

    1. [Aspect2] around advise
    2.  
    3. [Aspect2] before advise
    4.  
    5. [Aspect1] around advise
    6.  
    7. [Aspect1] around advise2
    8.  
    9. [Aspect1] after advise
    10.  
    11. [Aspect1] afterReturning advise
    12.  
    13. [Aspect2] around advise2
    14.  
    15. [Aspect2] after advise
    16.  
    17. [Aspect2] afterReturning advise

参考:Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置的更多相关文章

  1. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  2. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  3. Spring AOP 的@Aspect

    Spring AOP 的@Aspect   转自:http://blog.csdn.net/tanghw/article/details/3862987 从Spring 2.0开始,可以使用基于sch ...

  4. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  5. spring bean的介绍以及xml和注解的配置方法

    5.Bean 下边我们来了解一下Bean的: Bean的作用域Bean的生命周期Bean的自动装配Resources和ResourceLoader 5.1Bean容器的初始化 Bean容器的初始化 两 ...

  6. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  7. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  8. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  9. Spring配置文件中bean标签中init-method和destroy-method和用注解方式配置

    Person类: public class Person {       private int i = 0;          public Person(){           System.o ...

随机推荐

  1. P1024 一元三次方程求解(二分答案)

    思路: 求这个根,然后有一个关键的条件|x1-x2|>=1,然后就是从-100,枚举到+100,每次二分(i, i+1)注意如果f(i)*f(i+1)>0则不进行二分,如果,你觉得这样的值 ...

  2. 【转】curl命令总结,Http Post_Get 常用

    curl命令总结 curl 是一个利用URL语法在命令行方式下工作的文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE ...

  3. docker 10 docker的镜像原理

    镜像是什么? 镜像是一个轻量级,可执行的软件包,用来打包运行环境和基于运行环境开发的软件包,它包含某个软件运行环境的所有内容.包括代码,运行时的库,配置文件和环境变量 UnionFs(联合文件系统) ...

  4. java 1.5 自动拆箱和装箱的注意事项

    背景 java1.5后引入了自动装箱和自动拆箱的概念 自动拆箱:将引用类型转化为基本数据类型 自动装箱:将基本数据类型装为引用类型 但是实际使用中,什么情况自动拆箱什么情况自动装箱呢? 自动装箱 In ...

  5. 江苏省选2019Round1游记

    JSOI2019R1过去了. Day0 打板子.看白书. 晚上太热了,楼下还在打铁,睡不着. 折腾到好晚才勉强睡着. Day1 早上好困啊. 开T1.看起来T1的60分很可做的样子?5min打完了(为 ...

  6. [Micropython]TPYBoard v102 DIY照相机

    摄像头(CAMERA或WEBCAM)又称为电脑相机.电脑眼.电子眼等,是一种视频输入设备,被广泛的运用于视频会议,安防系统 .图像采集系统. 环境监控 .工业现场过程控制 等方面.本实验用TPYBoa ...

  7. 快速搭建日志系统——ELK STACK

    什么是ELK STACK ELK Stack是Elasticserach.Logstash.Kibana三种工具组合而成的一个日志解决方案.ELK可以将我们的系统日志.访问日志.运行日志.错误日志等进 ...

  8. C#调用迅雷下载,调用迅雷影音播放

    方法很多种,这里介绍一种,通过命令行参数调用. try { ]; Process.Start(thunderPath, "http://www.baidu.com/abc.exe" ...

  9. 高并发下的Java数据结构(List、Set、Map、Queue)

    由于并行程序与串行程序的不同特点,适用于串行程序的一些数据结构可能无法直接在并发环境下正常工作,这是因为这些数据结构不是线程安全的.本节将着重介绍一些可以用于多线程环境的数据结构,如并发List.并发 ...

  10. Autofac踩坑经历

    背景 接口框架使用反射,动态生成Controller,使用Autofac进行依赖注入,并替换默认DependencyResolver及IControllerFactory,Controller实例化代 ...