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
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
true
compile
If you are using eclipse, this will ensure that the weaving is done when deploying inside eclipse:
http://www.eclipse.org/ajdt/