重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Advice
分类Apsect
中不同类型Advice
执行顺序
Aspect
中Advice
执行顺序
Aspect1
为高优先级,Aspect2
为低优先级
Aspect1
为低优先级,Aspect2
为高优先级
本文主要验证Spring AOP中Advice
的执行顺序问题。(Spring版本: 5.3.23)
创新互联专注于企业成都营销网站建设、网站重做改版、青秀网站定制设计、自适应品牌网站建设、H5开发、成都商城网站开发、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为青秀等各大城市提供网站开发制作服务。
Advice
分类Spring AOP中Advice
可分为如下五类:
@Around
@Before
@AfterReturning
@AfterThrowing
@After
Advice
相关概念参考
Apsect
中不同类型Advice
执行顺序package sakura.springinaction;
@SpringBootApplication
@EnableAspectJAutoProxy
public class MySpringApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringApplication.class, args);
}
}
package sakura.springinaction.controller;
@Controller
@Slf4j
public class IndexController {
@GetMapping("/time")
@ResponseBody
public String time() {
LocalDateTime now = LocalDateTime.now();
String nowTime = now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
log.info("Current time: " + nowTime);
return nowTime;
}
}
Apsect1
@Slf4j
@Component
@Aspect
public class Aspect1 {
// 定义 Point Cut 切面
@Pointcut("execution(public * sakura.springinaction.controller.*.*(..))")
public void controllerLayer() {
}
// 定义Advice
@Before("controllerLayer()")
private void beforeAdvice2() {
log.info("Aspect_1 # @Before");
}
@After("controllerLayer() && @annotation(getMapping)")
private void afterAdvice1(GetMapping getMapping) {
log.info("Aspect_1 # @afterAdvice" + " path: " + Arrays.toString(getMapping.value()));
}
@AfterReturning(pointcut = "controllerLayer()", returning = "val")
private void afterReturningAdvice(Object val) {
log.info("Aspect_1 # @AfterReturning" + " returnValue: " + val);
}
@AfterThrowing(pointcut = "controllerLayer()", throwing = "thrower")
private void afterThrowingAdvice(Throwable thrower) {
log.info("Aspect_1 # @AfterThrowing" + " thrower: " + thrower.getClass().getName());
}
@Around("controllerLayer() && @annotation(getMapping)")
private Object aroundAdvice(ProceedingJoinPoint pjp, GetMapping getMapping) throws Throwable {
// Around 前置处理
Stopwatch stopwatch = Stopwatch.createStarted();
log.info("Aspect_1 # @Around-Before" + " methodName: " + pjp.getSignature().getName() + ", path: " + Arrays.toString(getMapping.value()));
Object result = pjp.proceed();
// Around 后置处理
log.info("Aspect_1 # @Around-After" + " methodName: " + pjp.getSignature().getName() + ", runTime: " + stopwatch.elapsed(TimeUnit.NANOSECONDS));
return result;
}
}
在 发起请求(http://localhost:8080/time)
后,日志输出如图:
在同一个切面(Apsect
)定义中对于同一个Join Point
而言,不同类型的Advice
执行先后顺序依次是:
@Around
前置处理@Before
@AfterReturning
/@AfterThrowing
@After
@Around
后置置处理优先级说明:
- 对于进入
Join Point
的Advice
而言(比如:@Around
前置处理,@Before
),优先级越高,越先执行;- 对于从
Join Point
出来的Advice
而言(比如:@Around
后置处理,@After
),优先级越高,越后执行;- 优先级从高到低依次为:
@Around
,@Before
,@After
,@AfterReturning
,@AfterThrowing
;
PS:
如果在同一个切面(
Apsect
)中定义了两个同类型的Advice
(比如定义两个@Before
), 对于某个Join Point
而言这两个Advice
都匹配,那么这两个Advice
执行的先后顺序是无法确定的。
Aspect
中Advice
执行顺序问: 当不同的Aspect
中的Advice
都匹配到了同一个Join Point
,那么那个Aspect
中的Advice
先执行,那个后执行呢?
答: 不确定 ,但是可以通过在class上添加注解@Order
指定优先级确定执行顺序(参考文档)
Aspect1
为高优先级,Aspect2
为低优先级Aspect1
类似,再定义一个切面类Aspect2
,如下package sakura.springinaction.advice;
import org.springframework.core.annotation.Order;
@Slf4j
@Component
@Aspect
@Order(2)
public class Aspect2 {
// 定义Advice
@Before("sakura.springinaction.advice.Aspect1.controllerLayer()")
private void beforeAdvice2() {
log.info("Aspect_2 # @Before");
}
@After("sakura.springinaction.advice.Aspect1.controllerLayer() && @annotation(getMapping)")
private void afterAdvice1(GetMapping getMapping) {
log.info("Aspect_2 # @afterAdvice" + " path: " + Arrays.toString(getMapping.value()));
}
@AfterReturning(pointcut = "sakura.springinaction.advice.Aspect1.controllerLayer()", returning = "val")
private void afterReturningAdvice(Object val) {
log.info("Aspect_2 # @AfterReturning" + " returnValue: " + val);
}
@AfterThrowing(pointcut = "sakura.springinaction.advice.Aspect1.controllerLayer()", throwing = "thrower")
private void afterThrowingAdvice(Throwable thrower) {
log.info("Aspect_2 # @AfterThrowing" + " thrower: " + thrower.getClass().getName());
}
@Around("sakura.springinaction.advice.Aspect1.controllerLayer() && @annotation(getMapping)")
private Object aroundAdvice(ProceedingJoinPoint pjp, GetMapping getMapping) throws Throwable {
Stopwatch stopwatch = Stopwatch.createStarted();
log.info("Aspect_2 # @Around-Before" + " methodName: " + pjp.getSignature().getName() + ", path: " + Arrays.toString(getMapping.value()));
Object result = pjp.proceed();
log.info("Aspect_2 # @Around-After" + " methodName: " + pjp.getSignature().getName() + ", runTime: " + stopwatch.elapsed(TimeUnit.NANOSECONDS));
return result;
}
}
Aspect1
添加@Order
注解指定优先级,如下@Slf4j
@Component
@Aspect
@Order(1)
public class Aspect1 {
//...
}
此时,Aspect1
的优先级比Aspect2
的优先级高。
实验结果如下:
说明:
高优先级的
Aspect1
中的@Around
前置处理和@Before
先于低优先级的Aspect2
执行,而@AfterReturning
,@After
和@Around
后置处理,则低优先级的Aspect2
先执行。
Aspect1
为低优先级,Aspect2
为高优先级Aspect
中@Order
注解优先级,如下:@Slf4j
@Component
@Aspect
@Order(2)
public class Aspect1 {
//...
}
@Slf4j
@Component
@Aspect
@Order(1)
public class Aspect2 {
//...
}
实验结果如下:
Aspect
中的Advice
都匹配到了同一个Join Point
,不同Aspect
中的Advice
执行顺序不确定。Aspect
类上添加注解@Order
指定优先级,确定执行顺序,执行顺序满足如下规律
@Around
前置处理 和@Before
两种Advice
而言,所在的Aspect
优先级越高,越先执行@AfterReturning
,@AfterThrowing
,@After
和@Around
后置处理 类型的Advice
而言,所在的Aspect
优先级越高,越后执行本文主要目的是记录学习过程,加深对知识点理解; 如有行文有误,望指正。