java - 如何更改由相同注释(例如 AspectJ 中的 @Around)注释的两个或多个建议的执行顺序?
问题描述
这是我的代码:
@Pointcut("execution(* *(..))")
public void cutPointToken() {}
@Pointcut("execution(* *(..))")
public void cutPointEmptyParam() {}
@Around("cutPointToken()")
public Object authenticateToken(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("authenticate -- start --");
...
Object o = joinPoint.proceed();
LOGGER.info("authenticate -- end --");
return o;
}
@Around("cutPointEmptyParam()")
public Object checkParameter(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.info("check param -- start --");
...
Object o = joinPoint.proceed();
LOGGER.info("check param -- end --");
return o;
}
我有:
authenticate -- start --
check param -- start --
...
check param -- end --
authenticate -- end --
预期的:
check param -- start --
authenticate -- start --
...
authenticate -- end --
check param -- end --
如何更改这两种方法的执行顺序?
在方法上和另一个上尝试了@Order
注释,但它不起作用。@Order(1)
checkParameter
@Order(2)
解决方案
使用@Order
注释的想法是正确的,但是,将其放在类级别,如文档7.2.4.7 建议排序状态。
这是通过实现 org.springframework.core 以正常的 Spring 方式完成的。方面类中的有序接口或使用 Order 注释对其进行注释。
由于未注册为 bean,因此在带有注释的方法上的放置@Aspect
将不起作用。在1.9.2中查找@Order
注释。@Autowired部分。
@Order
注释可以在目标类级别声明,也可以在方法上声明@Bean
......
@Aspect
@Order(1)
public class CheckParameterAspect {
@Around("cutPointEmptyParam()")
public Object checkParameter(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
@Aspect
@Order(2)
public class AuthenticateTokenAspect {
@Around("cutPointToken()")
public Object authenticateToken(ProceedingJoinPoint joinPoint) throws Throwable {
//...
}
}
编辑:订购0
似乎是可能的。
推荐阅读
- php - 无法使用 PHP 发送电子邮件
- unity3d - 为什么我们统一使用 GetComponent 方法从我们的脚本中访问其他组件,我们不能只创建一个该类型的对象吗?
- list - 无法在 Flutter 中使用 .map 函数
- pageload - 为什么页面无法在移动浏览器上加载?
- pine-script - Pinescript 从图表中隐藏 Plotchar 统计信息
- django - 如何使用 Vuejs 和 Django Rest 获取当前经过身份验证的用户 ID ...?
- sql - 如何在SQL中比较同一列中的两个日期
- node.js - 获取 Gists 的 Cors 选项
- html - 滚动时如何使导航栏保持固定
- discord.py - 我在处理这段代码时遇到了问题