0

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

我正在用aspectj-maven-plugin1.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>

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

但是,我的 Maven 脚本正在使用 Mavensurefire插件执行集成测试(即 Spring Boot 测试),并且旨在验证事务是否正在回滚以防引发异常的测试失败。

这是我的@Configuration文件:

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

// some beans not related to persistence

}

我在这里想念什么?

4

2 回答 2

0

您不需要 AspectJ Maven 或任何其他类型的编译时编织工具即可使其在 Spring 中工作。只需通过 LTW(加载时编织)使用 AspectJ

于 2019-10-18T07:09:04.393 回答
0

为了配置 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>
于 2019-11-15T02:49:45.333 回答