Search code examples
javaspringmaven-2aspectjspring-aop

How to set springframework @Transactional with AspectJ


I want to use spring-aspects to make my methods transactional, but without using Spring AOP (Spring AOP works just fine with: <tx:annotation-driven/>). I'm using Maven to manage my project.

Is there a way to do compile time weaving on my project classes so "they are Transactional". I was trying to use the Mojo's AspectJ Maven Plugin, but without any good results.

Please help.


Solution

  • I figured it out. Maven plugin works fine but the problem was with my spring config: I had:

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    

    What I needed was:

    <bean id="transactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean class="org.springframework.transaction.aspectj.AnnotationTransactionAspect" factory-method="aspectOf">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
    

    Now it works fine. And performace of my @Transactional methods improved and that what I was aming for.

    Here is my maven aspectj plugin config:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.3</version>
        <configuration>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
           </aspectLibraries>
            <source>1.5</source>
            <showWeaveInfo>true</showWeaveInfo>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    hope this helps someone.