Spring @Transactional + AspectJ 编译时编织不起作用

Spring @Transactional + AspectJ Compile Time Weaving does not work

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

我正在使用 aspectj-maven-plugin 版本 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>

编译部分工作正常,我看到我的 classes 被编织在日志中,我也可以在 class 文件中看到:一堆 ...$AjcClosure... classes.

但是,我的 maven 脚本正在使用 maven surefire 插件执行集成测试(spring 启动测试)以及旨在验证事务是否被回滚以防万一的测试正在抛出异常,正在失败。

这是我的 @Configuration 文件:

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

// some beans not related to persistence

}

我在这里错过了什么?

您不需要 AspectJ Maven 或任何其他类型的编译时织入工具即可使其在 Spring 中运行。只需使用 AspectJ via LTW (load-time weaving) 即可。

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

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

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

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