Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

aop

aspectj

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?

like image 801
MikePatel Avatar asked Aug 07 '12 16:08

MikePatel


1 Answers

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.

like image 176
kriegaex Avatar answered Nov 15 '22 10:11

kriegaex