@org.springframework.context.annotation.Configuration @ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class }) @ConditionalOnSingleCandidate(DataSource.class) @EnableConfigurationProperties(MybatisProperties.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class) public class MybatisAutoConfigurationimplements InitializingBean{
@ConditionalOnSingleCandidate(DataSource.class) DataSourceTransactionManagerAutoConfiguration
public class DataSourceTransactionManagerAutoConfiguration{ @Configuration @ConditionalOnSingleCandidate(DataSource.class) static class DataSourceTransactionManagerConfiguration{
@ConditionalOnSingleCandidate(DataSource.class) @ConditionalOnSingleCandidate
The condition will also match if multiple matching bean instances are already contained in the BeanFactory but a primary candidate has been defined; essentially, the condition match if auto-wiring a bean with the defined type will succeed.
/** * @Description 数据源的配置 * @Author CJB * @Date 2020/3/9 13:45 */ @Configuration @MapperScan(basePackages = {"com.vivachek.service.dao","com.vivachek.service.dao2"}) public class DatasourceConfig{ /** * 注入数据源1 */ @ConfigurationProperties(prefix = "spring.datasource1") @Bean(value = "dataSource1") public DataSource dataSource1(){ return new DruidDataSource(); } /** * 第二个数据源 */ @Bean(name = "dataSource2") @ConfigurationProperties(prefix = "spring.datasource2") public DataSource dataSource2(){ return new DruidDataSource(); } /** * 动态数据源 * * @return */ @Bean @Primary public DynamicDataSource dynamicDataSource(){ DynamicDataSource dataSource = new DynamicDataSource(); //默认数据源,在没有切换数据源的时候使用该数据源 dataSource.setDefaultTargetDataSource(dataSource2()); HashMap<Object, Object> map = Maps.newHashMap(); map.put("dataSource1", dataSource1()); map.put("dataSource2", dataSource2()); //设置数据源Map,动态切换就是根据key从map中获取 dataSource.setTargetDataSources(map); return dataSource; } }
DynamicDataSource
DataSourceInitializerInvoker
造成的循环依赖了,果不其然,其中确实依赖了DataSource,源码如下: DataSourceInitializerInvoker(ObjectProvider<DataSource> dataSource, DataSourceProperties properties, ApplicationContext applicationContext) { this.dataSource = dataSource; this.properties = properties; this.applicationContext = applicationContext; }
问题找到了,如何解决?此时心中一万个草泥马奔腾,怎么解决呢?
哈哈,此时插播一条广告,本人的独立博客已经发布了很多文章,感兴趣的可以收藏一下,【关于我】中有我的微信联系方式,欢迎交流。
DataSourceInitializerInvoker
是什么时候注入到IOC容器中的,因此我们找到了 DataSourceAutoConfiguration
,继而找到了 DataSourceInitializationConfiguration
这个配置类,源码如下: @Configuration @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) @EnableConfigurationProperties(DataSourceProperties.class) @Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class }) public class DataSourceAutoConfiguration{ @Configuration @Conditional(EmbeddedDatabaseCondition.class) @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) @Import(EmbeddedDataSourceConfiguration.class) protected static class EmbeddedDatabaseConfiguration{ } @Configuration @Conditional(PooledDataSourceCondition.class) @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) @Import({ DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class, DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class }) protected static class PooledDataSourceConfiguration{ } } @Configuration @Import({ DataSourceInitializerInvoker.class, DataSourceInitializationConfiguration.Registrar.class }) class DataSourceInitializationConfiguration{
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
,这什么鬼,不多说了,相信读过SpringBoot源码的都知道,这个配置类根本不起作用啊,那还要它干嘛,直接搞掉不就完事了。好了,分析到这里终于知道解决的方案了,搞掉 DataSourceAutoConfiguration
,怎么搞呢?一个注解搞定。 //排除配置类 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})