Spring Transaction Management using Aspects

For the sake of doing dependency injection into some Hibernate loaded domain classes (see previous post), I was forced to change an application to use AspectJ for it’s aspect weaving. When I did this, it broke my existing usage of the Spring @Transaction annotation.

Here’s what I had to do to fix it.

1. Define my transaction as annotation driven in the application context file.

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>

2. Manually set the transactionManager on the AnnotationTransactionAspect class. Supposedly, this is a bug in Spring pre 2.5.x, but I was still encountering it even in 2.5.5.

<bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf" dependency-check="none">
<property name="transactionManager" ref="transactionManager"/>
</bean>

Leave a Comment