简体   繁体   English

XML文件中的Springboot多数据源

[英]Springboot multi datasource in xml file

I want to create a restService with Springboot. 我想用Springboot创建一个restService。 But I want a configurable datasource, I want to have the capacity to add a new datasource when I want. 但是我想要一个可配置的数据源,我想有能力在需要时添加新的数据源。 But I have this exception : 但是我有这个例外:

> No qualifying bean of type 'javax.sql.DataSource' available: expected
> single matching bean but found 2

And I'm using this code : 我正在使用此代码:

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

and : 和:

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

and in java code : 并在Java代码中:

public void setEntityManager(final HibernateEntityManagerFactory entityManager) {
    final RepositoryFactorySupport factorySupport = new JpaRepositoryFactory(entityManager.createEntityManager());

    controlRepository = factorySupport.getRepository(ObjControlRepository.class);

Thanks you for your help. 感谢您的帮助。

我只是为两个配置之一添加primary =“ true”,这是一个愚蠢的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM