mysql datasource property,资源注释:没有定义类型为[javax.sql.DataSource]的符合条件的bean:预期的单个匹配bean但是找到2...

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...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值