接口 TransactionManagementConfigurer
public interface TransactionManagementConfigurer
Interface to be implemented by @Configurationclasses annotated with @EnableTransactionManagementthat wish to or need to explicitly specify the defaultPlatformTransactionManagerbean to be used for annotation-driven transaction management, as opposed to the default approach of a by-type lookup. One reason this might be necessary is if there are twoPlatformTransactionManagerbeans present in the container.See @
EnableTransactionManagementfor general examples and context; seeannotationDrivenTransactionManager()for detailed instructions.Note that in by-type lookup disambiguation cases, an alternative approach to implementing this interface is to simply mark one of the offending
PlatformTransactionManager@Beanmethods as@Primary. This is even generally preferred since it doesn't lead to early initialization of thePlatformTransactionManagerbean.- 从以下版本开始:
- 3.1
- 作者:
- Chris Beams
- 另请参阅:
EnableTransactionManagement,Primary
方法概要
所有方法 实例方法 抽象方法 修饰符和类型 方法 说明 PlatformTransactionManagerannotationDrivenTransactionManager()Return the default transaction manager bean to use for annotation-driven database transaction management, i.e. when processing@Transactionalmethods.
方法详细资料
annotationDrivenTransactionManager
PlatformTransactionManager annotationDrivenTransactionManager()
Return the default transaction manager bean to use for annotation-driven database transaction management, i.e. when processing@Transactionalmethods.There are two basic approaches to implementing this method:
1. Implement the method and annotate it with
In this case, the implementing@Bean@Configurationclass implements this method, marks it with@Beanand configures and returns the transaction manager directly within the method body:@Bean @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return new DataSourceTransactionManager(dataSource()); }2. Implement the method without
@Beanand delegate to another existing@Beanmethod@Bean public PlatformTransactionManager txManager() { return new DataSourceTransactionManager(dataSource()); } @Override public PlatformTransactionManager annotationDrivenTransactionManager() { return txManager(); // reference the existingIf taking approach #2, be sure that only one of the methods is marked with@Beanmethod above }@Bean!In either scenario #1 or #2, it is important that the
PlatformTransactionManagerinstance is managed as a Spring bean within the container as allPlatformTransactionManagerimplementations take advantage of Spring lifecycle callbacks such asInitializingBeanandBeanFactoryAware.