Announcement Announcement Module
Collapse
No announcement yet.
Strange behaviour with Ordered.HIGHEST_PRECEDENCE Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange behaviour with Ordered.HIGHEST_PRECEDENCE

    Hi

    I have a strange behaviour when ordering advices with the Ordered inteface using Spring AOP. I have created a test project to reproduce it

    I have two aspects, AspectA and AspectB, both the same, except of AspectA having HIGHEST_PRECEDENCE as a default value for order and AspectB having LOWEST_PRECEDENCE as the default.

    AspectA:

    Code:
    @Aspect
    public class AspectA implements Ordered {
    
    	int order = HIGHEST_PRECEDENCE;
    	
    	public int getOrder() {
    		return order;
    	}
    	
    	public void setOrder(int order) {
    		this.order = order;
    	}
    
    	@Around("execution(* *..*Service.*(..))")
    	public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    		Object o = null;
    		
    		System.out.println("AspectA: Before service call....");
    		o = proceedingJoinPoint.proceed();
    		System.out.println("AspectA: After service call....");
    		
    		return o;
    	}
    
    }
    AspectB:

    Code:
    @Aspect
    public class AspectB implements Ordered {
    
    	int order = LOWEST_PRECEDENCE;
    	
    	public int getOrder() {
    		return order;
    	}
    	
    	public void setOrder(int order) {
    		this.order = order;
    	}
    
    	@Around("execution(* *..*Service.*(..))")
    	public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    		Object o = null;
    		
    		System.out.println("AspectB: Before service call....");
    		o = proceedingJoinPoint.proceed();
    		System.out.println("AspectB: After service call....");
    		
    		return o;
    	}
    
    }
    When I manually setup the order in the spring configuration (applicationContext-AB.xml)

    Code:
    	<aop:aspectj-autoproxy/>
    
    	<bean id="targetService" class="app.TargetServiceImpl"/>
    
    	<bean class="app.AspectA">
    		<property name="order" value="1"/>
    	</bean>
    		
    	<bean class="app.AspectB">
    		<property name="order" value="2"/>
    	</bean>
    Then the chain is setup as expected:

    AspectA: Before service call....
    AspectB: Before service call....
    executing target service ....
    AspectB: After service call....
    AspectA: After service call....

    When I use the default ... by not specifying an order (applicationContext-Default.xml)

    Code:
    	<bean class="app.AspectA">
    	</bean>
    		
    	<bean class="app.AspectB">
    	</bean>
    then the chain still setup as expected:

    AspectA: Before service call....
    AspectB: Before service call....
    executing target service ....
    AspectB: After service call....
    AspectA: After service call....

    if I only use the default for AspectA, which is HIGHEST_PRECEDENCE (applicationContext-DefaultAnd1.xml)

    Code:
    	<bean class="app.AspectA">
    	</bean>
    		
    	<bean class="app.AspectB">
    		<property name="order" value="1"/>
    	</bean>
    then I get the following interceptor chain

    AspectB: Before service call....
    AspectA: Before service call....
    executing target service ....
    AspectA: After service call....
    AspectB: After service call....

    where B is before A, which I don't understand.

    what's even more confusing:if I use the MIN_VALUE constant (same as HIGHEST_PRECEDENCE) hardcoded in the config for the order value of AspectA (applicationContext-NegativeValue.xml)

    Code:
    	<bean class="app.AspectA">
    		<property name="order" value="-2147483648"/>
    	</bean>
    		
    	<bean class="app.AspectB">
    		<property name="order" value="1"/>
    	</bean>
    then it again works as expected:

    AspectA: Before service call....
    AspectB: Before service call....
    executing target service ....
    AspectB: After service call....
    AspectA: After service call....


    Do I misunderstand something. Shouldn't HIGHEST_PRECEDENCE mean first in the interceptor chain ? Why is it working with the negative value hardcoded ?

    Any suggestions ?

    Thanks!
Working...
X