方法级别上的spring注释建议顺序
我已经创建了两个自定义spring方法级别上的spring注释建议顺序,spring,aop,Spring,Aop,我已经创建了两个自定义spring@注释。我需要在方法级别而不是类级别定义这些注释的顺序。@order,在方法层面上也起作用吗 @Aspect @Component public class ABC { private ThreadLocal<logDTO> myThreadLocal; @Before("@annotation(CommunicationLogInit)") public void Campaign
@注释。我需要在方法级别而不是类级别定义这些注释的顺序。@order
,在方法层面上也起作用吗
@Aspect
@Component
public class ABC {
private ThreadLocal<logDTO> myThreadLocal;
@Before("@annotation(CommunicationLogInit)")
public void CampaignLogsInit(){
myThreadLocal = new ThreadLocal<logDTO>();
myThreadLocal.set(new logDTO());
}
@Around("@annotation(CommunicationAudit)")
public Object generateLog(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
int counter = 0;
for(Parameter parameter : method.getParameters()){
Annotation eventName = parameter.getAnnotation(EventName.class);
Annotation rtnInfo = parameter.getAnnotation(RtnInfo.class);
if(eventName!=null){
System.out.println(joinPoint.getArgs()[counter]);
myThreadLocal.get().setName("ABC");
}
}
}
}
@方面
@组成部分
公共课ABC{
私有线程本地线程和本地线程;
@在(@annotation(CommunicationLogInit)”之前
公共关系{
myThreadLocal=新的ThreadLocal();
myThreadLocal.set(新logDTO());
}
@周围(“@annotation(CommunicationAudit)”)
公共对象generateLog(ProceedingJoinPoint joinPoint)抛出可丢弃的{
MethodSignature=(MethodSignature)joinPoint.getSignature();
Method=signature.getMethod();
int计数器=0;
for(参数:method.getParameters()){
Annotation eventName=parameter.getAnnotation(eventName.class);
注释rtnInfo=parameter.getAnnotation(rtnInfo.class);
if(eventName!=null){
System.out.println(joinPoint.getArgs()[counter]);
myThreadLocal.get().setName(“ABC”);
}
}
}
}
Spring AOP手册在以下部分回答了您的问题:
建议订购
当多条建议都希望在同一连接点上运行时,会发生什么情况?SpringAOP遵循与AspectJ相同的优先级规则来确定通知执行的顺序。优先级最高的通知在“传入”过程中首先运行(因此,给定两条before通知,优先级最高的通知将首先运行)。从连接点“在退出”时,优先级最高的通知将最后运行(因此,给定两条after通知,优先级最高的通知将第二次运行)
当在不同方面定义的两条通知都需要在同一连接点上运行时,除非您另外指定,否则执行顺序是未定义的。您可以通过指定优先级来控制执行顺序。这是通过在aspect类中实现org.springframework.core.Ordered
接口,或者使用Order注释对其进行注释,以正常的Spring方式完成的。给定两个方面,从Ordered.getValue()
(或注释值)返回较低值的方面具有较高的优先级
当在同一方面中定义的两条通知都需要在同一连接点上运行时,顺序是未定义的(因为无法通过对javac编译类的反射来检索声明顺序)。考虑将此类建议方法折叠成每个方面类中的每个连接点的一个建议方法,或者将这些建议重构为可以在方面级别排序的单独方面类。
我想你对最后一段特别感兴趣。你能出示你的自定义注释代码吗欢迎阅读。如果你知道什么是A,然后把你的问题变成A,那就好了。否则人们就得猜你在问什么。所以让我猜猜:你的方面实际上起作用了,两种建议方法都被触发了,但不是按你喜欢的顺序触发的?你想知道你是否可以用@Order
来做这个?