SpringAop实例@Aspect、@Before、@AfterReturning@Ar。。
⽤过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;
ption.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,回车:
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,回车:
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 将按照以下的顺序进⾏执⾏:
正常情况:
情况⼆: 同⼀个⽅法被多个Aspect类拦截
此处举例为被两个aspect类拦截。
有些情况下,对于两个不同的aspect类,不管它们的advice使⽤的是同⼀个pointcut,还是不同的pointcut,都有可能导致同⼀个⽅法被多个aspect类拦截。那么,在这种情况下,这多个Aspect类中的advice⼜是按照怎样的顺序进⾏执⾏的呢?请看:
pointcut类保持不变
添加⼀个新的aspect类
package test;spring framework开发参考手册
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,回车:
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 先执⾏。
不信?你把服务务器重新启动⼀下,再试试,说不定你就会看到如下的执⾏结果:
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,回车:
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 的执⾏顺序呢?
⽅法有两种:
实现Ordered接⼝,实现它的getOrder()⽅法
给aspect添加@Order注解,该注解全称为:annotation.Order
不管采⽤上⾯的哪种⽅法,都是值越⼩的 aspect 越先执⾏。
⽐如,我们为 apsect1 和 aspect2 分别添加 @Order 注解,如下:
1. @Order(5)
2. @Component
3. @Aspect
4. public class Aspect1 {
5. // ...
6. }
7. @Order(6)
8. @Component
9. @Aspect
10. public class Aspect2 {
11. // ...
12. }
这样修改之后,可保证不管在任何情况下, 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();给删掉,那么,我们看到的输出结果将是:
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 官⽅资料:
其中,AOP的执⾏顺序章节为:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。