1. 报错信息
系统错误,错误信息:org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'defaultTx' is expected to be of type 'org.springframework.transaction.PlatformTransactionManager' but was actually of type 'org.springframework.transaction.support.TransactionTemplate'2.2. 原因
使用了自定义的事务模板,样例代码如下
@Bean(name = TransactionConfig.DEFAULT_TX) @Primary public TransactionTemplate transaction(@Qualifier("firstDataSource") DataSource firstDataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(firstDataSource); return new TransactionTemplate(dataSourceTransactionManager); } @Bean(name = TransactionConfig.SECOND_TX) public TransactionTemplate secondTransaction(@Qualifier("secondDataSource") DataSource secondDataScoure) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(secondDataScoure); return new TransactionTemplate(dataSourceTransactionManager); }
3. 解决方法
@Bean(name = DEFAULT_TX) @Primary public DataSourceTransactionManager firstDataSourceTransaction(@Qualifier("firstDataSource") DataSource firstDataSource) { return new DataSourceTransactionManager(firstDataSource); } @Bean(name = SECOND_TX) public DataSourceTransactionManager secondDataSourceTransaction(@Qualifier("secondDataSource") DataSource secondDataSource) { return new DataSourceTransactionManager(secondDataSource); }