2

I am trying to synchronize declarative transactions (i.e. methods annotated with @Transactional) using AspectJ like so:

...
import org.aspectj.lang.annotation.Aspect;
...

@Component
@Aspect
public class TransactionMonitor extends TransactionSynchronizationAdapter {

    @Before("execution(@org.springframework.transaction.annotation.Transactional * *.*(..))")
    private void registerTransactionSynchronizationOnAnnotation(JoinPoint joinPoint) {
        TransactionSynchronizationManager.registerSynchronization(this);
    }
}

This currently fails with java.lang.IllegalStateException: Transaction synchronization is not active which indicates that the synchronization is not run inside the transaction execution, but before. I want to ensure that this is the other way round, of course.

I found this answer, however @Order(Ordered.LOWEST_PRECEDENCE) had no effect, and

@DeclarePrecedence(
        "org.springframework.transaction.aspectj.AnnotationTransactionAspect, xxx.xxx.TransactionMonitor, *"
)

led to this during startup:

java.lang.IllegalArgumentException: DeclarePrecedence not presently supported in Spring AOP 

I have the feeling this is AOP and AspectJ not being happy with each other, but I am not sure. I am thankful for any ideas.

EDIT: I have to use @EnableTransactionManagement(proxyTargetClass = true), can this be related to the issue?

Yanick Nedderhoff
  • 1,174
  • 3
  • 18
  • 35

1 Answers1

1

For @DeclarePrecedence you need to switch to native AspectJ. Spring AOP is just "AOP lite" and technologically has little in common with AspectJ other than its syntax which is basically an AspectJ subset. The Spring manual describes how to use native AspectJ in Spring via LTW (load-time weaving). Precedence declaration for Spring components rather works using @Order, BTW.

I am not a Spring user at all, but as for declarative transaction management, it already knows proxy-based Spring AOP versus native AspectJ mode, see EnableTransactionManagement.mode and the enum constants in AdviceMode. Besides, EnableTransactionManagement also has an order property. Reading Javadoc and the Spring manual helps, I guess.

kriegaex
  • 63,017
  • 15
  • 111
  • 202