spring 配置双数据源 Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type

oyhk 学习笔记

简单看下这段代码,是spring下面配置双数据源

下面是使用了注解方式去配置数据源


@Configuration
public class DataSourceConfig {

    @Value(value = "${sql.server.url}")
    public String sqlServerUrl;
    @Value(value = "${sql.server.username}")
    public String sqlServerUsername;
    @Value(value = "${sql.server.password}")
    public String sqlServerPassword;

    @Bean(name = "dataSourceNative")
    public DataSource dataSourceNative() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(sqlServerUrl);
        dataSource.setUsername(sqlServerUsername);
        dataSource.setPassword(sqlServerPassword);
        return dataSource;
    }


    @Value(value = "${sqlite.url}")
    public String sqLiteUrl;
    @Bean(name = "dataSourceSqLite")
    public DataSource dataSourceSqLite() {
        SQLiteDataSource sqLiteDataSource = new SQLiteDataSource();
        sqLiteDataSource.setUrl(sqLiteUrl);
        return sqLiteDataSource;
    }
}

引用方式

    @Autowired
    @Qualifier("dataSourceNative")
    private DataSource dataSourceNative;
    @Autowired
    @Qualifier("dataSourceSqLite")
    private DataSource dataSourceSqLite;
spring 当两个同类型的bean时,使用@Autowired 时 要配合使用@Qualifier 一起使用才可以注入成功 


当你启动时,报了以下的错误

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: dataSourceNative,dataSourceSqLite
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968) ~[spring-context-4.1.7.RELEASE.jar:4.1.7.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:67) ~[spring-boot-autoconfigure-1.2.5.RELEASE.jar:1.2.5.RELEASE]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
	... 65 common frames omitted

为什么呢?原因是在配置数据源时需要添加@Primary,下面是正确的代码

@Configuration
public class DataSourceConfig {

    @Value(value = "${sql.server.url}")
    public String sqlServerUrl;
    @Value(value = "${sql.server.username}")
    public String sqlServerUsername;
    @Value(value = "${sql.server.password}")
    public String sqlServerPassword;

    @Bean(name = "dataSourceNative")
    @Primary
    public DataSource dataSourceNative() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(sqlServerUrl);
        dataSource.setUsername(sqlServerUsername);
        dataSource.setPassword(sqlServerPassword);
        return dataSource;
    }


    @Value(value = "${sqlite.url}")
    public String sqLiteUrl;
    @Bean(name = "dataSourceSqLite")
    public DataSource dataSourceSqLite() {
        SQLiteDataSource sqLiteDataSource = new SQLiteDataSource();
        sqLiteDataSource.setUrl(sqLiteUrl);
        return sqLiteDataSource;
    }
}
谢谢。