方法级别的 spring 注释建议顺序 [英] spring annotation advice order on method level

查看:30
本文介绍了方法级别的 spring 注释建议顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 2 个自定义弹簧 @annotations.我需要在方法级别而不是类级别定义这些注释的顺序.@order 是否也适用于方法级别?

I have created 2 custom spring @annotations. I need to define the order of these annotations, on method-level, not on class-level. Does @order, work on the method level, too?

 @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");
    }
    }
    }
    }

推荐答案

Spring AOP 手册在 建议排序:

The Spring AOP manual answers your question in section advice ordering:

当多个通知都想在同一个连接点运行时会发生什么?Spring AOP 遵循与 AspectJ 相同的优先级规则来确定通知执行的顺序.最高优先级的建议首先在in the way in"运行(因此,给定两条 before 建议,具有最高优先级的建议最先运行).从连接点出路",最高优先级通知最后运行(因此,给定两条后通知,优先级最高的将第二运行).

Advice Ordering

What happens when multiple pieces of advice all want to run at the same join point? Spring AOP follows the same precedence rules as AspectJ to determine the order of advice execution. The highest precedence advice runs first "on the way in" (so, given two pieces of before advice, the one with highest precedence runs first). "On the way out" from a join point, the highest precedence advice runs last (so, given two pieces of after advice, the one with the highest precedence will run second).

当在不同方面定义的两条通知都需要在同一个连接点运行时,除非您另外指定,否则执行顺序是未定义的.您可以通过指定优先级来控制执行顺序.这是通过在方面类中实现 org.springframework.core.Ordered 接口或使用 Order 注释对其进行注释以普通 Spring 方式完成的.给定两个方面,从 Ordered.getValue() 返回较低值的方面(或注释值)具有更高的优先级.

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise, the order of execution is undefined. You can control the order of execution by specifying precedence. This is done in the normal Spring way by either implementing the org.springframework.core.Ordered interface in the aspect class or annotating it with the Order annotation. Given two aspects, the aspect returning the lower value from Ordered.getValue() (or the annotation value) has the higher precedence.

当在同一个方面定义的两条通知都需要在同一个连接点运行时,排序是未定义的(因为对于 javac 编译的类,无法通过反射检索声明顺序).考虑将这些通知方法合并为每个方面类中每个连接点的一个通知方法,或者将这些通知部分重构为可以在方面级别排序的单独方面类.

When two pieces of advice defined in the same aspect both need to run at the same join point, the ordering is undefined (since there is no way to retrieve the declaration order through reflection for javac-compiled classes). Consider collapsing such advice methods into one advice method per join point in each aspect class or refactor the pieces of advice into separate aspect classes that you can order at the aspect level.

我认为您对最后一段特别感兴趣.

I think the last paragraph is of particular interest to you.

这篇关于方法级别的 spring 注释建议顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆