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

后端 未结 2 1142
日久生厌
日久生厌 2021-01-12 19:26

I am in the process of adding Spring declarative transactions via the @Transactional annotation to an existing Java project.

When I ran into a problem (unrelated to

相关标签:
2条回答
  • 2021-01-12 20:03

    Strange, it sounds like you have this configuration:

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

    (Transaction support using AspectJ, not JDK proxies)

    Since your config doesn't have a mode attribute, the default should kick in (proxy mode). But AnnotationTransactionAspect is the exact aspect used by the aspectj mode.

    0 讨论(0)
  • 2021-01-12 20:15

    To get aspectj transactions working with java config.

    @EnableWebMvc
    @Configuration
    @ComponentScan("com.yourdomain")
    @EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
    public class ApplicationConfig extends WebMvcConfigurerAdapter {
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    
            //...
        }
    
        @Bean
        public JpaTransactionManager transactionManager() {
    
            JpaTransactionManager bean = new JpaTransactionManager(entityManagerFactory().getObject());
            return bean ;
        }
    
        @Bean
        public AnnotationTransactionAspect annotationTransactionAspect() {
    
            AnnotationTransactionAspect bean = AnnotationTransactionAspect.aspectOf();
            bean.setTransactionManager(transactionManager());
            return bean;
        }
    }
    

    If you are using maven:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
            <aspectLibraries>
                <aspectLibrary>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aspects</artifactId>
                </aspectLibrary>
            </aspectLibraries>
            <complianceLevel>1.8</complianceLevel>
            <source>1.8</source>
            <target>1.8</target>
            <showWeaveInfo>true</showWeaveInfo>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    If you are using eclipse, this will ensure that the weaving is done when deploying inside eclipse:

    http://www.eclipse.org/ajdt/

    0 讨论(0)
提交回复
热议问题