资源注释:没有定义类型为[javax.sql.DataSource]的符合条件的bean:预期的单个匹配bean但是找到2 [英] Resource Annotation: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2

查看:9115
本文介绍了资源注释:没有定义类型为[javax.sql.DataSource]的符合条件的bean:预期的单个匹配bean但是找到2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于Spring Java的配置来使用Spring Data配置多个数据库。
在配置文件中,我为 MySQL 创建两个数据源 MSSQL-服务器。当尝试使用 @Resource 注释向实体管理员注入依赖项时,我得到以下异常:

  org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义类型为[javax.sql.DataSource]的符合条件的bean:预期的单一匹配bean,但发现2:mysql_datasource,secure_datasource 
在org.springframework .beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1016)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
在org.springframework .beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815)
在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:743)

以下是我的代码:

  @Bean(name =secure_datasource)
pub lic DataSource dataSource(){
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(environment.getProperty(sc.db.url));
dataSource.setDriverClass(environment.getProperty(sc.db.driver.class));
dataSource.setUser(environment.getProperty(sc.db.username));
dataSource.setPassword(environment.getProperty(sc.db.password));
dataSource.setIdleConnectionTestPeriod(60);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(7);
dataSource.setMinPoolSize(1);
return dataSource;
} catch(Exception ex){
throw new RuntimeException(ex);
}
}

.................

@Bean(name =mysql_datasource )
public DataSource dataSource(){
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(environment.getProperty(db.url));
dataSource.setDriverClass(environment.getProperty(db.driver.class));
dataSource.setUser(environment.getProperty(db.username));
dataSource.setPassword(environment.getProperty(db.password));
dataSource.setIdleConnectionTestPeriod(60);
dataSource.setMaxPoolSize(100);
dataSource.setMaxStatements(50);
dataSource.setMinPoolSize(10);
return dataSource;
} catch(Exception ex){
throw new RuntimeException(ex);
}
}

.......

@Resource(value =mysql_datasource)
@Bean(name =entity_manager_factory)
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
factoryBean.setPackagesToScan(environment.getProperty(package.scan));
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
return factoryBean;
}

我也试图使用 @Qualifier 注释建议我这个链接,但仍然得到错误。 使用相同类型的2个bean: Spring中的javax.sql.DataSource

解决方案

我有同样的问题,很多头痛后,我偶然发现这个 doc 使我感到非常愚蠢: (



所有您需要的是您的一个DataSources上的@Primary,而Spring-Boot将不再困惑...这是我的一个配置...休息是非常相同的,指向其他数据库,没有@Primary他们...

  @Configuration 
@EnableTransactionManagement
@EntityScan(basePackages = {somepackage.entities})
@EnableJpaRepositories(entityManagerFactoryRef =emfDB1,transactionManagerRef =tmDB1,basePackages = {somepackage.repositories})
类Persistenc eDB1 {
@Bean
@Primary
DataSource dsDB1(){
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(jdbc:mysql:// someserver:3306 / proativo);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(com.mysql.jdbc.Driver);
return dataSource;
}

@Bean
@Primary
LocalContainerEntityManagerFactoryBean emfDB1(){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dsDB1());
entityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());

entityManagerFactoryBean.setPersistenceXmlLocation(classpath:META-INF / DB1-persistence.xml);

属性jpaProperties = new Properties();
jpaProperties.put(eclipselink.weaving,false);
jpaProperties.put(eclipselink.logging.level,SEVERE); // SEVERE / FINEST

entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}

@Bean
@Primary
JpaTransactionManager tmDB1(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emfDB1()。getNativeEntityManagerFactory());
return transactionManager;
}
}

编辑:
忘了提了:可能由于我的配置类完成的方式,在@EnableAutoConfiguration上排除一些类的方法对我来说不起作用。


I am using Spring Java Based configuration for configure multiple database with Spring Data. In the configuration file, i am creating two data source for MySQL and MSSQL-Server. When trying to inject dependency to the entity manager using @Resource annotation i am getting following exception:

org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: mysql_datasource,secure_datasource
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1016)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:743)

Following is my Code:

@Bean(name="secure_datasource")
public DataSource dataSource(){
    try{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl(environment.getProperty("sc.db.url"));
        dataSource.setDriverClass(environment.getProperty("sc.db.driver.class"));
        dataSource.setUser(environment.getProperty("sc.db.username"));
        dataSource.setPassword(environment.getProperty("sc.db.password"));
        dataSource.setIdleConnectionTestPeriod(60);
        dataSource.setMaxPoolSize(10);
        dataSource.setMaxStatements(7);
        dataSource.setMinPoolSize(1);
        return dataSource; 
    }catch(Exception ex){
        throw new RuntimeException(ex);
    }
}

.................

@Bean(name="mysql_datasource")
public DataSource dataSource(){
    try{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl(environment.getProperty("db.url"));
        dataSource.setDriverClass(environment.getProperty("db.driver.class"));
        dataSource.setUser(environment.getProperty("db.username"));
        dataSource.setPassword(environment.getProperty("db.password"));
        dataSource.setIdleConnectionTestPeriod(60);
        dataSource.setMaxPoolSize(100);
        dataSource.setMaxStatements(50);
        dataSource.setMinPoolSize(10);
        return dataSource; 
    }catch(Exception ex){
        throw new RuntimeException(ex);
    }
}

.......

@Resource(value="mysql_datasource")
@Bean(name="entity_manager_factory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
    factoryBean.setPackagesToScan(environment.getProperty("package.scan"));
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    return factoryBean;
}

I am also trying to use @Qualifier annotation as suggest i this link, but still getting error. Using 2 beans of the same type: javax.sql.DataSource in Spring

解决方案

I had the same problem and after a lot of headache I stumbled upon this doc that made me feel really dumb :(

All you need is @Primary on one of your DataSources and Spring-Boot won't get confused anymore... Here is one of my configurations... The rest are pretty much identical, pointing to other DBs and with no @Primary on them...

@Configuration
@EnableTransactionManagement
@EntityScan(basePackages = {"somepackage.entities"})
@EnableJpaRepositories(entityManagerFactoryRef = "emfDB1", transactionManagerRef = "tmDB1", basePackages = {"somepackage.repositories"})
class PersistenceDB1 {
    @Bean
    @Primary
    DataSource dsDB1() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://someserver:3306/proativo");
        dataSource.setUsername("username");
        dataSource.setPassword("password");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }

    @Bean
    @Primary
    LocalContainerEntityManagerFactoryBean emfDB1() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dsDB1());
        entityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());

        entityManagerFactoryBean.setPersistenceXmlLocation("classpath:META-INF/DB1-persistence.xml");

        Properties jpaProperties = new Properties();
        jpaProperties.put("eclipselink.weaving", "false");
        jpaProperties.put("eclipselink.logging.level", "SEVERE"); // SEVERE / FINEST

        entityManagerFactoryBean.setJpaProperties(jpaProperties);
        return entityManagerFactoryBean;
    }

    @Bean
    @Primary
    JpaTransactionManager tmDB1() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emfDB1().getNativeEntityManagerFactory());
        return transactionManager;
    }
}

Edit: Forgot to mention: Probably due to the way my configuration classes are done, the method of excluding some classes on @EnableAutoConfiguration didn't work for me...

这篇关于资源注释:没有定义类型为[javax.sql.DataSource]的符合条件的bean:预期的单个匹配bean但是找到2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆