springboot+mybats多数据源时依赖循环问题

环境

maven依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.microservice
    dbandcache
    0.0.1-SNAPSHOT
    dbandcache
    Demo project for Spring Boot


    1.8



    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.0.1
    

    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
        io.springfox
        springfox-swagger2
        2.7.0
    

    
        io.springfox
        springfox-swagger-ui
        2.7.0
    

    
    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    
    
        com.alibaba
        druid
        1.0.26
    

    
        mysql
        mysql-connector-java
        runtime
    

    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.3.2
    

    
        org.springframework.boot
        spring-boot-devtools
        true
    




    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    



多数据源配置类

@Configuration
@MapperScan(basePackages = "com.microservice.dbandcache.mapper")
public class MybatisConfig
{
	@Autowired
	private Environment environment;

	/**
	 * 单一数据源
	 * @return
	 * @throws Exception
	 */
//	@Bean
//	public DataSource dataSource () throws Exception{
//		Properties properties = new Properties();
//		properties.put("driverClassName",environment.getProperty("dbandcache1.jdbc.driverClassName"));
//		properties.put("url",environment.getProperty("dbandcache1.jdbc.url"));
//		properties.put("username",environment.getProperty("dbandcache1.jdbc.username"));
//		properties.put("password",environment.getProperty("dbandcache1.jdbc.passwrd"));
//
//		return DruidDataSourceFactory.createDataSource(properties);
//	}


	/********多数据源开始********/
	@Bean
	public DataSource micorservicedb1 () throws Exception{
		Properties props = new Properties();
		props.put("driverClassName",environment.getProperty("dbandcache1.jdbc.driverClassName"));
		props.put("url",environment.getProperty("dbandcache1.jdbc.url"));
		props.put("username",environment.getProperty("dbandcache1.jdbc.username"));
		props.put("password",environment.getProperty("dbandcache1.jdbc.passwrd"));

		return DruidDataSourceFactory.createDataSource(props);
	}

	@Bean
	public DataSource microservicedb2 () throws Exception{
		Properties properties = new Properties();
		properties.put("driverClassName",environment.getProperty("dbandcache2.jdbc.driverClassName"));
		properties.put("url",environment.getProperty("dbandcache2.jdbc.url"));
		properties.put("username",environment.getProperty("dbandcache2.jdbc.username"));
		properties.put("password",environment.getProperty("dbandcache2.jdbc.passwrd"));

		return DruidDataSourceFactory.createDataSource(properties);
	}

	//@Qualifier注解用来区别同类的bean
	@Bean
	@Primary
	public DynamicDataSource dataSource(@Qualifier("micorservicedb1") DataSource micorservicedb1,@Qualifier("microservicedb2") DataSource microservicedb2){
		HashMap targetDataSources = new HashMap<>();

		targetDataSources.put(DataBaseType.micorservicedb1,micorservicedb1);
		targetDataSources.put(DataBaseType.microservicedb2,microservicedb2);

		DynamicDataSource dynamicDataSource = new DynamicDataSource();
		//AbstractRoutingDataSource#setTargetDataSources 设置目标数据源s
		dynamicDataSource.setTargetDataSources(targetDataSources);
		//设置默认的数据源
		dynamicDataSource.setDefaultTargetDataSource(micorservicedb1);
		return dynamicDataSource;
	}

	/**********************动态数据源结束****************************/

	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
	{
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		//设置数据源
		sqlSessionFactoryBean.setDataSource(dataSource);
		//指定基础包
		sqlSessionFactoryBean.setTypeAliasesPackage("com.microservice.dbandcache.model");
		//指定mapper位置
		sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
				.getResources("classpath:mapping/*.xml"));

		return sqlSessionFactoryBean.getObject();
	}
}

启动后出现依赖循环异常,许多博客的解决办法是:在application.propertites文件中添加

	sprign.datasource.initialize=false

或者升级springboot的


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE

看了看都不能解决我的问题,经查找发现了解决办法:

@Import(MybatisConfig.class)
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
//@MapperScan(basePackages = "com.microservice.dbandcache.mapper")
public class DbandcacheApplication
{

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

}

题外话

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)中的exclude解释:

SpringBootApplication注解的源码

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    //排除某些类
    Class[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class[] scanBasePackageClasses() default {};
}


@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AliasFor {
    @AliasFor("attribute")
    String value() default "";

    @AliasFor("value")
    String attribute() default "";

    Class annotation() default Annotation.class;
}

你可能感兴趣的:(spring,boot)