Spring @Transactional + AspectJ编译时编织不起作用 [英] Spring @Transactional + AspectJ Compile Time Weaving does not work

查看:142
本文介绍了Spring @Transactional + AspectJ编译时编织不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆标有@Transactional批注的方法,然后它们进行自调用,并且某些方法是私有的,因此我想在Spring中使用AspectJ风格的事务管理.

我正在使用 aspectj-maven-plugin 版本1.11编译代码:

 < plugin>< groupId> org.codehaus.mojo</groupId>< artifactId> aspectj-maven-plugin</artifactId>< version> 1.11</version><配置>< proc>无</proc>< forceAjcCompile> true</forceAjcCompile>< complianceLevel> $ {java.version}</complianceLevel>< source> $ {java.version}</source>< target> $ {java.version}</target>< showWeaveInfo> true</showWeaveInfo>< aspectLibraries>< aspectLibrary>< groupId> org.springframework</groupId>< artifactId> spring-aspects//artifactId></aspectLibrary></aspectLibraries>< sources>< source>< basedir> $ {project.build.directory}/generated-sources/annotations</basedir></source>< source>< basedir> $ {project.build.directory}/generate-sources/delombok</basedir></source></sources>< testSources>< source>< basedir>$ {project.build.directory}/generation-test-sources/delombok</basedir></source></testSources></configuration><执行><执行><目标>< goal>编译</goal><目标>测试编译</目标></goals></execution></executions></plugin> 

编译部分工作正常,我在日志中看到了我的类,并且在类文件中也看到了:一堆 ... $ AjcClosure ... 类./p>

但是,然后,我的maven脚本正在使用maven surefire 插件执行集成测试(即春季启动测试),该测试旨在验证是否在发生异常的情况下是否回滚了事务被抛出,失败了.

这是我的 @Configuration 文件:

  @Configuration@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)公共类MyAppConfig {//一些与持久性无关的bean} 

我在这里想念什么?

解决方案

为了配置CTW,我必须在配置中配置以下bean:

  @Beanpublic PlatformTransactionManager txManager(DataSource dataSource){DataSourceTransactionManager txManager =新的DataSourceTransactionManager(dataSource);AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);返回txManager;} 

此外,我的maven文件演变为:

 < plugin>< groupId> org.codehaus.mojo</groupId>< artifactId> aspectj-maven-plugin</artifactId>< version> 1.11</version><配置>< proc>无</proc>< Xlint>忽略</Xlint>< forceAjcCompile> true</forceAjcCompile>< complianceLevel> $ {java.version}</complianceLevel>< source> $ {java.version}</source>< target> $ {java.version}</target>< showWeaveInfo> true</showWeaveInfo>< sources/>< testSources/>< aspectLibraries>< aspectLibrary>< groupId> org.springframework</groupId>< artifactId> spring-aspects//artifactId></aspectLibrary></aspectLibraries></configuration><执行><执行>< id>编译源</id><目标>< goal>编译</goal></goals><配置>< weaveDirectories>< weaveDirectory> $ {project.build.outputDirectory}</weaveDirectory></weaveDirectories></configuration></execution><执行>< id>编译测试源</id><目标><目标>测试编译</目标></goals><配置>< weaveDirectories>< weaveDirectory> $ {project.build.testOutputDirectory}</weaveDirectory></weaveDirectories></configuration></execution></executions><依赖关系><依赖关系>< groupId> org.aspectj</groupId>< artifactId> aspectjtools</artifactId>< version> $ {aspectj.version}</version></dependency><依赖关系>< groupId> org.aspectj</groupId>< artifactId> aspectjrt</artifactId>< version> $ {aspectj.version}</version></dependency></dependencies></plugin> 

I have a bunch of methods that are marked with @Transactional annotation and then they do self-invocation and some of the methods are private, so I want to use AspectJ flavour of transaction management with Spring.

I am compiling my code with aspectj-maven-plugin version 1.11:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <proc>none</proc>
                    <forceAjcCompile>true</forceAjcCompile>
                    <complianceLevel>${java.version}</complianceLevel>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <showWeaveInfo>true</showWeaveInfo>


                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>


                    <sources>
                        <source>
                            <basedir>${project.build.directory}/generated-sources/annotations</basedir>
                        </source>
                        <source>
                            <basedir>${project.build.directory}/generated-sources/delombok</basedir>
                        </source>
                    </sources>
                    <testSources>
                        <source>
                            <basedir>
                                ${project.build.directory}/generated-test-sources/delombok
                            </basedir>
                        </source>
                    </testSources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The compile part works fine and I see my classes being woven in the logs and I can also see that in the class files: a bunch of ...$AjcClosure... classes.

But then, my maven script is executing integration tests (which are spring boot tests) using maven surefire plugin and the tests that are meant to verify if the transaction is being rollbacked in case an exception is being thrown, are failing.

Here is my @Configuration file:

@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
public class MyAppConfig {

// some beans not related to persistence

}

What am I missing here?

解决方案

In order to configure CTW I had to configure following bean in my config:

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        DataSourceTransactionManager txManager = new DataSourceTransactionManager(dataSource);
        AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);
        return txManager;
    }

Also, my maven file evolved into:

           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.11</version>
                <configuration>
                    <proc>none</proc>
                    <Xlint>ignore</Xlint>
                    <forceAjcCompile>true</forceAjcCompile>
                    <complianceLevel>${java.version}</complianceLevel>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <sources/>
                    <testSources/>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                </configuration>
                <executions>
                    <execution>
                        <id>compile-sources</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <weaveDirectories>
                                <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                            </weaveDirectories>
                        </configuration>
                    </execution>
                    <execution>
                        <id>compile-test-sources</id>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <weaveDirectories>
                                <weaveDirectory>${project.build.testOutputDirectory}</weaveDirectory>
                            </weaveDirectories>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

这篇关于Spring @Transactional + AspectJ编译时编织不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆