我想用Springboot创建一个restService。但是我想要一个可配置的数据源,我希望能够在需要时添加新的数据源。但我有这个例外:
> No qualifying bean of type 'javax.sql.DataSource' available: expected
> single matching bean but found 2
我正在使用此代码:
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="ORACLE"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:9999:TEST"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes-->
<property name="packagesToScan" value="com.orange.aurore.model.entity"/>
<property name="persistenceUnitName" value="msPersistenceUnit" />
</bean>
<bean id="controllerService" class="...controller.impl.ControllerServiceImpl">
<property name="entityManager" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource"/>
</bean>
和:
<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:9999:TEST"/>
<property name="username" value="test2"/>
<property name="password" value="test2"/>
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<!-- spring based scanning for entity classes-->
<property name="packagesToScan" value="com.orange.aurore.model.entity"/>
<property name="persistenceUnitName" value="msPersistenceUnit2" />
</bean>
<bean id="controllerService2" class="...controller.impl.ControllerServiceImpl">
<property name="entityManager" ref="entityManagerFactory2" />
<property name="dataSource" ref="dataSource2"/>
</bean>
并在java代码中:
public void setEntityManager(final HibernateEntityManagerFactory entityManager) {
final RepositoryFactorySupport factorySupport = new JpaRepositoryFactory(entityManager.createEntityManager());
controlRepository = factorySupport.getRepository(ObjControlRepository.class);
感谢您的帮助。
答案 0 :(得分:0)
这是一个愚蠢的错误,我为我的两个配置之一添加了primary =“true”。