如何使用 AspectJ 设置 springframework @Transactional

发布于 2024-08-20 02:54:37 字数 356 浏览 7 评论 0原文

我想使用 spring-aspects 使我的方法具有事务性,但不使用 Spring AOP(Spring AOP 可以很好地配合: )。 我正在使用 Maven 来管理我的项目。

有没有办法在我的项目类上进行编译时编织,以便“它们是事务性”。 我尝试使用 Mojo 的 AspectJ Maven 插件,但没有任何好的结果。

请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北渚 2024-08-27 02:54:37

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

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

我需要的是:

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

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

这是我的 Mavenspectj 插件配置:

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

希望这对某人有帮助。

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.

糖果控 2024-08-27 02:54:37

也许你可以尝试这个:

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

maybe you can try this:

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
物价感观 2024-08-27 02:54:37

以下是我给出的关于如何在 java 配置中执行相同操作的答案的链接:

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

希望它有帮助

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

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

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文