用Spring创建两个数据源

时间:2016-04-21 02:29:08

标签: java spring jndi persistence-unit

我试图在我的Spring项目中添加两个数据源,但是,我收到此错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ajaxController': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: entityManagerFactory,entityManagerFactoryDataGathering
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: entityManagerFactory,entityManagerFactoryDataGathering
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:572)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:531)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:697)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:670)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:354)
    ... 22 more

以下是我dispatcher-servlet.xml的相关部分:

<jee:jndi-lookup id="benDS" jndi-name="jdbc/ben"
    expected-type="javax.sql.DataSource" />

<jee:jndi-lookup id="benDS_data_gathering"
    jndi-name="jdbc/ben_data_gathering" expected-type="javax.sql.DataSource" />

<!-- Session Factory Declaration -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="benDS" />
    <property name="persistenceUnitName" value="ben" />
    <property name="packagesToScan" value="com.ben.*" />
</bean>

<bean id="entityManagerFactoryDataGathering"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="benDS_data_gathering" />
    <property name="persistenceUnitName" value="ben_data_gathering" />
    <property name="packagesToScan" value="com.ben.*" />
</bean>

<tx:annotation-driven />

<!-- Transaction Manager is defined -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="transactionManagerDataGathering" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryDataGathering" />
</bean>

这是我的UserDAO.java

@PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "ben")
@Qualifier(value = "entityManagerFactory")
private EntityManager em;

@PersistenceContext(type = PersistenceContextType.TRANSACTION, unitName = "ben_data_gathering")
@Qualifier(value = "entityManagerFactoryDataGathering")
private EntityManager em1;

在Tomcat中,这是我的context.xml

<Resource name="jdbc/ben" auth="Container" type="javax.sql.DataSource"
    maxActive="-1" maxIdle="-1" maxWait="-1" autoReconnect="true"
    username="example" password="example" driverClassName="com.mysql.jdbc.Driver"
    spring.datasource.testOnBorrow="true"
    spring.datasource.validationQuery="SELECT 1"
    url="example" />

<Resource name="jdbc/ben_data_gathering" auth="Container" type="javax.sql.DataSource"
    maxActive="-1" maxIdle="-1" maxWait="-1" autoReconnect="true"
    username="example" password="example" driverClassName="com.mysql.jdbc.Driver"
    spring.datasource.testOnBorrow="true"
    spring.datasource.validationQuery="SELECT 1"
    url="example" />

最后,我的persistence.xml

<persistence-unit name="ben" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/comp/env/jdbc/ben</non-jta-data-source>
    <properties>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.jdbc.batch_size" value="20" />
        <property name="hibernate.cache.use_query_cache" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.cache.use_second_level_cache"
            value="false" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="jadira.usertype.autoRegisterUserTypes"
            value="true" />
    </properties>
</persistence-unit>

<persistence-unit name="ben_data_gathering" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/comp/env/jdbc/ben_data_gathering</non-jta-data-source>
    <properties>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.jdbc.batch_size" value="20" />
        <property name="hibernate.cache.use_query_cache" value="false" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.cache.use_second_level_cache"
            value="false" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="jadira.usertype.autoRegisterUserTypes"
            value="true" />
    </properties>
</persistence-unit>

我一直在寻找帮助,但我无法取得任何进展。我怎样才能正确添加第二个数据源?我只用一个就可以做到这一点。

1 个答案:

答案 0 :(得分:1)

创建bean&#39; ajaxController&#39;时发生错误而不是在创建UserDAO时。你有没有在ajaxController中连接EntityManager?

Error creating bean with name 'ajaxController'