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:
AspectB: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; } }
When I manually setup the order in the spring configuration (applicationContext-AB.xml)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; } }
Then the chain is setup as expected: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>
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)
then the chain still setup as expected:Code:<bean class="app.AspectA"> </bean> <bean class="app.AspectB"> </bean>
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)
then I get the following interceptor chainCode:<bean class="app.AspectA"> </bean> <bean class="app.AspectB"> <property name="order" value="1"/> </bean>
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)
then it again works as expected:Code:<bean class="app.AspectA"> <property name="order" value="-2147483648"/> </bean> <bean class="app.AspectB"> <property name="order" value="1"/> </bean>
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!


Reply With Quote