springboot多数据源死循环,springboot动态据源死循环

springboot多数据源死循环,springboot动态据源死循环

The dependencies of some of the beans in the application context form a cycle

 

 

================================

©Copyright 蕃薯耀 2020-04-24

https://www.cnblogs.com/fanshuyao/

 

一、问题描述

 

 

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration
┌─────┐
|  dynamicDataSource defined in class path resource [com/szpl/baOneMap/dataSource/DruidConfig.class]
↑     ↓
|  masterDataSource defined in class path resource [com/szpl/baOneMap/dataSource/DruidConfig.class]
↑     ↓
|  org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker

 

 

 多数据源(动态数据源)配置:

@Configuration
@Profile({"dev"})
public class DynamicDataSourceConfig{
    
    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource.master")
    public DataSource masterDataSource(){
        return DataSourceBuilder.create().build();
    }

    
    @Bean
    @ConfigurationProperties("spring.datasource.slave")
    @ConditionalOnProperty(prefix = "spring.datasource.slave", name = "enabled", havingValue = "true")
    public DataSource slaveDataSource(){
        return DataSourceBuilder.create().build();
    }

    
    @Bean(name = "dynamicDataSource")
    public DynamicDataSource dynamicDataSource(){
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource());
        targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource());
        
        return new DynamicDataSource(masterDataSource(), targetDataSources);
    }
    
    
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dynamicDataSource());
        
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));

        return sqlSessionFactory.getObject();
    }
    
    
    @Bean
    public SqlSessionTemplate sqlSessionTemplate() throws Exception {

        return new SqlSessionTemplate(sqlSessionFactory());
    }
    
    /**
     * 事务管理
     */
    @Bean(name="transactionManager")
    public PlatformTransactionManager transactionManager() throws Exception {

        return new DataSourceTransactionManager(dynamicDataSource());
    }
    
    
}

 

 

 

问题出现的原因是:在masterDataSource加了@Primary注解导致的。

 

 

 

二、解决方案:

 

方法一:

将注解@Primary放到dynamicDataSource上。

 

 方法二:

启动类加上:(exclude = {DataSourceAutoConfiguration.class})

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

}

 

 

================================

©Copyright 蕃薯耀 2020-04-24

https://www.cnblogs.com/fanshuyao/

 

posted @ 2020-04-24 10:03  蕃薯耀  阅读(1418)  评论(0编辑  收藏  举报