Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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");
    }
    }
    }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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).

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...