Spring プロジェクトに 2 つのデータ ソースを追加しようとしていますが、このエラーが発生します
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
my の関連部分は次の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>
私は助けを求めてSOの周りを探していましたが、進歩がありませんでした。2 番目のデータ ソースを正しく追加するにはどうすればよいですか? 1本でも問題なくできました。