AspectJ Load Time Weaving with Spring Transaction

2019-06-06 16:05发布

问题:

I'm attempting to enable load time weaving with Spring's transaction manager but without too much luck. Currently I'm just trying to run a simple em.persist() in a @Transactional method but it does not appear to running a transaction as seen through: TransactionSynchronizationManager.isActualTransactionActive()

My application context file contains :

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="TEST-pu"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" proxy-target-class="true"/>

And my pom.xml contains:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-agent</artifactId>
    <version>2.5.4</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.10</version>
</dependency>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <forkMode>once</forkMode>
        <argLine>
            -javaagent:${settings.localRepository}/org/springframework/spring-agent/2.5.4/spring-agent-2.5.4.jar
        </argLine>
        <useSystemClassloader>true</useSystemClassloader>
    </configuration>
</plugin> 

It would appear as if there is some issue with the setup and while I have come across quite a few examples of how to implement AspectJ / Load time weaving they all seem to be using Eclipse plugins which 1) I am trying to avoid using any sort of plugins and 2) I am using Intellij. Any help would be much appreciated.

Thanks.

回答1:

Have you added:

<context:load-time-weaver/>

to your setup?