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

后端 未结 2 1141
日久生厌
日久生厌 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: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:

    
        org.codehaus.mojo
        aspectj-maven-plugin
        1.7
        
            
                
                    org.springframework
                    spring-aspects
                
            
            1.8
            1.8
            1.8
            true
        
        
            
                
                    compile
                
            
        
    
    

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

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

提交回复
热议问题