If the order attribute is not configured for the @Transactional annotation, the Advisor that is responsible for the transaction attribute - AbstractPointcutAdvisor (in fact, one of the subclasses) will return Ordered .LOWEST_PRECEDENCE, which is defined as Integer.MAX_VALUE.
The adviser responsible for the AOP user advice, a subclass of the same AbstractPointcutAdvisor, will see if the actual Advice implements the ordered interface, and if so, the provided value will be used for sorting. If the AOP user tip does not implement a custom interface, the EA returns the same default Ordered.LOWEST_PRECEDENCE, and the sorting result becomes slightly unpredictable.
So, setting the order attribute for the @Transactional annotation, for example. like this
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd> ....... <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" order="[Order for @Transactional]"> <beans/>
and your custom implementation of AOP recommendations looks like this:
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.core.Ordered; @Aspect public class CustomAspect implements Ordered { @Around(value = "@annotation(CustomAnnotation)") public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable { ... } .... @Override public int getOrder() { return [Order for @CustomAnnotation]; } .... }
then you probably have all the freedom (but statically) with ordering throughout the application.
Under the hood is AspectJAwareAdvisorAutoProxyCreator , which on Proxy Initialization sorts Advisors using the org.springframework.aop.aspectj.autoproxy.AspectJPrecedenceComparator comparator, which delegates the OrderComparator sort. Later, after the actual execution, a specific implementation of AopProxy is executed for a specific method, an array of tips, which it calls interceptors, and this can be used to dynamically reorder, I think, but none of these things seem easily accessible and / or customizable to me.
My environment is Spring Beans, TX, AOP - all version 4.0.3. I also have two custom transaction managers, one related to Hibernate and one related to JDBC DataSource-bound, but I don't think it matters here.