Spring java-based configuration 配置transaction manager

项目中使用到了Spring-boot,我想用编程的方式来配置Spring 的transaction manager,开始的时候我配置了Hibernate的session Factory,和HibernateTransactionManager,在类名上加了@Configuration和@EnableTransactionManagement,程序运行的过程中提示有两个transaction manager,我想应该是Spring boot会自动注册一个PlatformTransactionManager的实例。后来经过进一步研究发现配置transaction manager需要实现TransactionManagementConfigurer接口和重写这个方法:

public PlatformTransactionManager annotationDrivenTransactionManager()

Spring API中的示例:

@Configuration
 @EnableTransactionManagement
 public class AppConfig implements TransactionManagementConfigurer {
     @Bean
     public FooRepository fooRepository() {
         // configure and return a class having @Transactional methods
         return new JdbcFooRepository(dataSource());
     }

     @Bean
     public DataSource dataSource() {
         // configure and return the necessary JDBC DataSource
     }

     @Bean
     public PlatformTransactionManager txManager() {
         return new DataSourceTransactionManager(dataSource());
     }

     @Override
     public PlatformTransactionManager annotationDrivenTransactionManager() {
         return txManager();
     }
 }


你可能感兴趣的:(Spring java-based configuration 配置transaction manager)