动态数据源循环依赖问题

Hello!欢迎各位新老朋友来看小弟博客,祝大家事业顺利,财源广进!!

主题:动态数据源循环依赖问题

错误重现
控制台打印的错误信息
在这里插入图片描述

第一:DynamicDataSourceConfig动态数据源类

package com.wonders.platform.webconfig;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * Filename:DynamicDataSourceConfig.java
 * Desc:配置多数据源, 当数据库连接多个数据源时,需要在这个地方注入
 * 在注入之前,application.yml文件内信息必须配置正确
 *
 * @author hcday
 * @author <a href="mailto:hcday@qq.com">hcday soo</a>
 * CreateTime:2017/9/28 18:23
 *
 * @version 1.0
 * @since 1.0
 *
 */
@Configuration
public class DynamicDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.druid.coc")
    public DataSource cocDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.second")
    public DataSource secondDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.third")
    public DataSource thirdDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.fourth")
    public DataSource fourthDataSource(){
        return DruidDataSourceBuilder.create().build();
    }
    @Bean
    @ConfigurationProperties("spring.datasource.druid.fifth")
    public DataSource fifthDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.sixth")
    public DataSource sixthDataSource(){ return DruidDataSourceBuilder.create().build(); }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.seventh")
    public DataSource seventhDataSource(){ return DruidDataSourceBuilder.create().build(); }

    @Bean
    @Primary
    public DynamicDataSource dataSource(DataSource cocDataSource,
//                                        DataSource h2DataSource,
                                        DataSource secondDataSource,
                                        DataSource thirdDataSource,
                                        DataSource fourthDataSource,
                                        DataSource fifthDataSource,
                                        DataSource sixthDataSource
                                        ) {
        Map<String, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceNames.FIRST, cocDataSource);
//        targetDataSources.put(DataSourceNames.H2, h2DataSource());
        targetDataSources.put(DataSourceNames.SECOND, secondDataSource);
        targetDataSources.put(DataSourceNames.THIRD, thirdDataSource);
        targetDataSources.put(DataSourceNames.FOURTH, fourthDataSource);
        targetDataSources.put(DataSourceNames.FIFTH, fifthDataSource);
        targetDataSources.put(DataSourceNames.SIXTH, sixthDataSource);
        return new DynamicDataSource(cocDataSource, targetDataSources);
    }

    /**
     * 自动识别使用的数据库类型
     * 在mapper.xml中databaseId的值就是跟这里对应,
     * 如果没有databaseId选择则说明该sql适用所有数据库
     * */
    @Bean
    public DatabaseIdProvider getDatabaseIdProvider(){
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("Oracle","oracle");
        properties.setProperty("MySQL","mysql");
        properties.setProperty("SqlServer","sqlserver");
        properties.setProperty("Hive","hive");
        properties.setProperty("DM8","dm8");
        properties.setProperty("DM DBMS","dm8");
//        properties.setProperty("DB2","db2");
//        properties.setProperty("Derby","derby");
//        properties.setProperty("H2","h2");
//        properties.setProperty("HSQL","hsql");
//        properties.setProperty("Informix","informix");
//        properties.setProperty("MS-SQL","ms-sql");
//        properties.setProperty("PostgreSQL","postgresql");
//        properties.setProperty("Sybase","sybase");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }
}

第二:WdcJarApplication启动类

/**
 * Copyright 2015-2017 the original author or Howai authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wonders;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.wonders.platform.webconfig.DynamicDataSourceConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;


/**
 * Wonders COC FRAME Application main for spring boot jar
 * Filename:WdcJarApplication.java
 * Desc: the main entrance of WDC to run, to debug
 * @SpringBootApplication      //全部注解,等价于以默认属性使用 @Configuration,@EnableAutoConfiguration 和 @ComponentScan
 *
 * @author hcday of Howai.org
 * @author <a href="mailto:hcday@qq.com">hcday soo</a>
 * CreateTime:2017-10-15 2:32 PM
 *
 * @version 1.0
 * @since 1.0
 *
 */

//@SpringBootApplication      //全部注解
@MapperScan(basePackages={"com.wonders.**.dao", "cn.wioc.**.dao"})
@EnableAutoConfiguration
@Configuration
@ComponentScan(value = {"com.wonders", "cn.wioc"})
//@ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM, classes={TypeExcludeFilter.class})})
@EnableScheduling
@EnableAsync
@ServletComponentScan
@EnableEncryptableProperties  //LiXinhai add for security 20211011
public class WdcJarApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WdcJarApplication.class);
    }

}


第三:解决办法

/**
 * Copyright 2015-2017 the original author or Howai authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wonders;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import com.wonders.platform.webconfig.DynamicDataSourceConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;


/**
 * Wonders COC FRAME Application main for spring boot jar
 * Filename:WdcJarApplication.java
 * Desc: the main entrance of WDC to run, to debug
 * @SpringBootApplication      //全部注解,等价于以默认属性使用 @Configuration,@EnableAutoConfiguration 和 @ComponentScan
 *
 * @author hcday of Howai.org
 * @author <a href="mailto:hcday@qq.com">hcday soo</a>
 * CreateTime:2017-10-15 2:32 PM
 *
 * @version 1.0
 * @since 1.0
 *
 */

//@SpringBootApplication      //全部注解
@Import({DynamicDataSourceConfig.class})
@MapperScan(basePackages={"com.wonders.**.dao", "cn.wioc.**.dao"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) // 更改这一行,就可以避免循环引用错误了。
@Configuration
@ComponentScan(value = {"com.wonders", "cn.wioc"})
//@ComponentScan(excludeFilters={@ComponentScan.Filter(type=CUSTOM, classes={TypeExcludeFilter.class})})
@EnableScheduling
@EnableAsync
@ServletComponentScan
@EnableEncryptableProperties  //LiXinhai add for security 20211011
public class WdcJarApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WdcJarApplication.class);
    }

}


注意改下面一行,就可以了。

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) // 更改这一行,就可以避免循环引用错误了。

添加一行,DynamicDataSourceConfig根据自己项目实际动态数据源配置文件名替换

@Import({DynamicDataSourceConfig.class})

好了,朋友们,点点关注不迷路,喜欢的朋友们还请留下您的小赞赞,小弟会更加努力更新!!如果感兴趣的话,欢迎关注小弟公众号 【科技脉搏】!!!🥳🥳🥳

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值