MikePatel
MikePatel

Reputation: 2672

AspectJ: execution order (precedence) for multiple advice within one aspect

Classes use compile time weaving.

Imagine I have the aspect class:

@Aspect
public class SecurityInterceptor {

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void beanAnnotatedWithController() {}

    @Pointcut("execution(public * *(..)) && args(*,httpReq)")
    public void publicMethods(HttpServletRequest httpReq) {}

    @Pointcut("beanAnnotatedWithController() && publicMethods(httpReq)")
    public void controllerMethods(HttpServletRequest httpReq) {}

    @Pointcut("execution(public * *(..)) && args(httpReq)")
    public void publicMethodsRequestOnly(HttpServletRequest httpReq) {}

    @Pointcut("beanAnnotatedWithController() && publicMethodsRequestOnly(httpReq)")
    public void controllerMethodsOneArg(HttpServletRequest httpReq) {}


    @Around(value = "controllerMethods(httpReq)")
    public Object populateSecurityContext(final ProceedingJoinPoint joinPoint, HttpServletRequest httpReq) throws Throwable {
        return popSecContext(joinPoint, httpReq);
    }

    @Around(value = "controllerMethodsOneArg(httpReq)")
    public Object populateSecurityContextOneArg(final ProceedingJoinPoint joinPoint, HttpServletRequest httpReq) throws Throwable {
        return popSecContext(joinPoint, httpReq);
    }

}

What is the correct way to use @DeclarePrecedence to determine the execution order?

Upvotes: 7

Views: 12378

Answers (3)

Kuldeep Singh
Kuldeep Singh

Reputation: 1274

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 via reflection for javac-compiled classes). Consider collapsing such advice methods into one advice method per joinpoint in each aspect class, or refactor the pieces of advice into separate aspect classes - which can be ordered at the aspect level.

from Spring AOP documentation here (section 6.2.4.7. Advice ordering) https://docs.spring.io/spring/docs/2.0.x/reference/aop.html

If it helps in case you came looking for this here.

Upvotes: 0

janmp
janmp

Reputation: 81

If you're are looking for the the order of multiple aspects, you can create an aspect like:

@Aspect
@DeclarePrecedence("AuthorizationAspect, MySpecialAspect, LastAspect")
public class CoordinationAspect {
    // empty
}

This will indeed work over multiple aspects. Inside a single aspect is another matter and can not be changed AFAIK, but I don't see why this would be an issue.

Upvotes: 7

kriegaex
kriegaex

Reputation: 67377

Please read paragraph "Advice precedence" in the language semantics section of the AspectJ documentation.

Precedence of aspects can be declared explicitly, precedence of advice within a single aspect is determined by rules described in the document and cannot be changed, AFAIK. So @DeclarePrecedence will not help you in this case, only changing the order of advice within the aspect file.

Upvotes: 7

Related Questions