如何使用AspectJ设置springframework @Transactional

Mar*_*zki 6 java spring maven-2 aspectj spring-aop

我想用来spring-aspects使我的方法事务性,但不使用Spring AOP(Spring AOP可以正常使用:) <tx:annotation-driven/>.我正在使用Maven来管理我的项目.

有没有办法在我的项目类上进行编译时编织,因此"它们就是Transactional".我试图使用Mojo的AspectJ Maven插件,但没有任何好的结果.

请帮忙.

Mar*_*zki 5

我想到了。Maven 插件工作正常,但问题出在我的 spring 配置上:我有:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
Run Code Online (Sandbox Code Playgroud)

我需要的是:

<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>
Run Code Online (Sandbox Code Playgroud)

现在它工作正常。我的@Transactional 方法的性能得到了改善,这正是我所追求的。

这是我的 maven aspectj 插件配置:

<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>
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人。