java 如何使用 AspectJ 设置 springframework @Transactional

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2192410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:57:48  来源:igfitidea点击:

How to set springframework @Transactional with AspectJ

javaspringmaven-2aspectjspring-aop

提问by Mariusz Zawadzki

I want to use spring-aspectsto 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.

我想用来spring-aspects使我的方法具有事务性,但不使用 Spring AOP(Spring AOP 与 : 配合得很好<tx:annotation-driven/>)。我正在使用 Maven 来管理我的项目。

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

有没有办法在我的项目类上进行编译时编织,所以“它们是Transactional”。我试图使用Mojo 的 AspectJ Maven Plugin,但没有任何好的结果。

Please help.

请帮忙。

采纳答案by Mariusz Zawadzki

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

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

<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.

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

Here is my maven aspectj plugin config:

这是我的 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>

hope this helps someone.

希望这有助于某人。

回答by YJiao

maybe you can try this:

也许你可以试试这个:

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

回答by Rian

Here is a link to the answer I gave on how to do the same in java config:

这是我给出的有关如何在 java config 中执行相同操作的答案的链接:

Spring @Transactional is applied both as a dynamic Jdk proxy and an aspectj aspect

Spring @Transactional 既用作动态 Jdk 代理又用作 aspectj 方面

Hope it helps

希望能帮助到你