Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

aop - spring annotation advice order on method level

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...