Home > Software engineering >  Why doesn't Spring support the order of annotations advice at the method level?
Why doesn't Spring support the order of annotations advice at the method level?

Time:12-23

@Component
@Aspect
@Slf4j(topic = "e")
public class NotVeryUsefulAspect{

    @Pointcut("within(com.lc.aop.for_source.service.impl.AAopServiceImpl)")
    public void pointCutWithinAAopService(){
    }

    @Pointcut("@within(com.lc.aop.for_source.service.XAnnotation)")
    public void pointCutAnnotation(){

    }

    @Before("pointCutWithinAAopService()")
    @Order(0)
    public void adviceBeforeAAopService(){
        log.debug("=======before aop service========");
    }

    @Before("pointCutAnnotation()")
    @Order(-1)
    public void adviceBeforeAAopService2(){
        log.debug("=======before aop annotation========");
    }
}

@Slf4j(topic = "e")
@Component("a")
@XAnnotation
public class AAopServiceImpl implements AopService {
    @Override
    public void m() {
        log.debug("a -AAopServiceImpl");
    }
}

Based on the advice-ordering

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 via Ordered or @Order.

I understand the @Order not work in this case? Why not suport the method level order?

I think this is a very simple function, but it can avoid some unnecessary misunderstandings about @Order

I would like to order advice by method level.

CodePudding user response:

That question should probably be posed to the chaps working on the springframework, that project is located at: https://github.com/spring-projects/spring-framework.

What you're asking for makes sense, but keep in mind that Order is meant to prioritize the loading of beans from the context, so it makes sense that Order needs to be applied to the Aspect and not the Pointcut itself.

CodePudding user response:

Well, the answer to your question is in the sentence directly before the one you quoted, in the very same paragraph of the very same info box:

When two pieces of the same type of advice (for example, two @After advice methods) defined in the same @Aspect class both need to run at the same join point, the ordering is undefined (since there is no way to retrieve the source code declaration order through reflection for javac-compiled classes).

Maybe next time you just read more carefully before quoting.

  • Related