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/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM