用过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() { }
}

添加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(111);
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 1
[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 1
[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 1
[Aspect2] before advise
[Aspect1] around advise 1
[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 1
[Aspect1] before advise
[Aspect2] around advise 1
[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 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] before advise
throw an exception
[Aspect1] after advise
[Aspect1] afterThrowing advise
[Aspect2] after advise
[Aspect2] afterThrowing advise

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

[Aspect1] around advise 1
[Aspect1] before advise
[Aspect2] around advise 1
[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(5)
@Component
@Aspect
public class Aspect1 {
// ...
} @Order(6)
@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();给删掉,那么,我们看到的输出结果将是:

[Aspect2] around advise 1
[Aspect2] before advise
[Aspect1] around advise 1
[Aspect1] around advise2
[Aspect1] after advise
[Aspect1] afterReturning advise
[Aspect2] around advise2
[Aspect2] after advise
[Aspect2] afterReturning advise

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

来源:http://blog.csdn.net/rainbow702/article/details/52185827

此文章来源于网络,版权不归本人所有。

Spring AOP @Before @Around @After 等 advice 的执行顺序的更多相关文章

  1. 转 Spring AOP @Before @Around @After 等 advice 的执行顺序

    转自:http://blog.csdn.net/rainbow702/article/details/52185827 情况一: 一个方法只被一个Aspect类拦截 正常情况:  异常情况:  情况二 ...

  2. spring Aop切面中的@Before @Around等执行顺序与请求参数统一解码

    1.背景 在实际开发中,我可能会对请求接口做统一日志输出,或者统一参数解析,验签,统一响应加密等,通常会用到aop,实际案例如下 2.代码 package com.qianxingniwo.log; ...

  3. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  4. Spring课程 Spring入门篇 6-1 Spring AOP API的PointCut、advice的概念及应用

    本节主要是模拟spring aop 的过程. 实现spring aop的过程 这一节老师虽然说是以后在工作中不常用这些api,实际上了解还是有好处的, 我们可以从中模拟一下spring aop的过程. ...

  5. Spring Aop(五)——给Advice传参

    转发:https://www.iteye.com/blog/elim-2395337 5 给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或Proce ...

  6. 正确理解Spring AOP中的Around advice

    Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了. 乍一看好像是Before ad ...

  7. Spring AOP 中 advice 的四种类型 before after throwing advice around

    spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...

  8. [转]彻底征服 Spring AOP 之 理论篇

    基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...

  9. Spring AOP基本概念

    Spring AOP基本概念 目录 Spring AOP定义 AOP基本术语 通知类型 AOP定义 AOP基本术语 切面( Aspect ):一个能横切多个对象的模块化的关注点.对Spring AOP ...

随机推荐

  1. Codeforces 1072 - A/B/C/D - (Done)

    链接:http://codeforces.com/contest/1072/ A - Golden Plate - [计算题] #include<bits/stdc++.h> using ...

  2. linux之cp和scp的使用

    cp 基本介绍 cp主要用于同一台服务器上,目录和文件的复制 表达式 cp [OPTIONS] SOURCE DEST --- 从源路径copy文件到目的路径 常用参数 -a same as -dpR ...

  3. numactl 修改 非统一内存访问架构 NUMA(Non Uniform Memory Access Architecture)模式

    当今数据计算领域的主要应用程序和模型可大致分为三大类: (1)联机事务处理(OLTP). (2)决策支持系统(DSS) (3)企业信息通讯(BusinessCommunications) 上述三类系统 ...

  4. 002-自定义打开terminal,以及快捷键,其他程序类似,ssh管理-sshpass, Shuttle

    一.利用Automator软件完成服务设定 1.使用Command+Space,打开Spotlight,搜索Automator 2.搜索到之后,双击打开,选择“服务[或快速操作]” 3.将“服务收到[ ...

  5. word2vec:将bin转换为txt

    转自:https://blog.csdn.net/u011684265/article/details/78024064 from gensim.models import word2vec mode ...

  6. ionic3 在ios9.0 系统下 会出现ReferenceError:Can't find variable:Intl 错误提示

    ionic3 框架开发app  在ios 9.0版本中 ReferenceError:Can't find variable:Intl 错误提示: 在index.html 文件中添加 <scri ...

  7. 记mysql中时间相关的一个奇怪问题

    发现mysql中类型为时间的字段,在查询时显示的时间是什么是依赖于客户端的,不同的客户端查同一个时间,可能在客户端显示的时间是不一样的.至于这个在哪里配置,以及服务端如何依据这个配置为客户端返回结果, ...

  8. linux下sublime输入中文

    title: linux下sublime输入中文 date: 2017-11-09 20:54:43 tags: sublime categories: 开发工具 archlinux系统 下载文件 g ...

  9. count列表中字符出现的次数

    如何count列表中字符出现的次数?可以将其生成一个字典.key是列表中的字符串,value是出现的次数 例如gen = [2, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 8, 4, ...

  10. 关于angular2 打包(一)

    在讲到angular2 及以上项目打包之前,我先讲一下.angular cli 拥有自己的打包工具,熟悉的可以直接上手.如果用不惯,也可以去使用webpack 之类的.内置的systemjs也是很好用 ...