• 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/

  • 相关阅读:
    Docker | 第二章:第一个Docker应用
    Docker | 第一章:Docker简介
    Docker | 第零章:前言
    SpringBoot | 第十五章:基于Postman的RESTful接口测试
    SpringBoot | 第十四章:基于Docker的简单部署
    SpringBoot | 第十三章:测试相关(单元测试、性能测试)
    2014年计划
    学习方法,工作方法 探讨
    2013年总结
    asp.net用Zxing库实现条形码输出
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/12765558.html
Copyright © 2020-2023  润新知