gpt4 book ai didi

java - 在 spring 应用程序中注入(inject) 2(不久的将来 3)个不同的 entityManagerFactory(或 entityManager)

转载 作者:行者123 更新时间:2023-11-29 07:39:01 31 4
gpt4 key购买 nike

我有 Spring + JPA (Hibernate) 项目,在该项目中我连接到 MsSQL 数据库,现在我需要打开一个新连接,但这次是针对 MySQL>。我正在使用 XML 配置

<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${spring.datasource.driverClassName}" />
....
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource" p:packagesToScan="com.wsg.admin.api.model"
p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter">

<property name="jpaProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
....
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

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

<!-- Configure the MySQL connection -->
<bean id="enduserDataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${enduser.db.driver}" />
....
</bean>

<bean id="enduserEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="enduserDataSource" p:packagesToScan="com.wsg.admin.api.model"
p:jpaVendorAdapter-ref="hibernateJpaVendorAdapter">

<property name="jpaProperties">
<props>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
....
</props>
</property>
</bean>

<tx:annotation-driven transaction-manager="enduserTransactionManager" />

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

然后我尝试使用片段注入(inject) entityManager

@Autowired
EntityManager entityManager;

但是我遇到了异常

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'brandServiceImpl': Unsatisfied dependency expressed through field 'entityManager'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2: org.springframework.orm.jpa.SharedEntityManagerCreator#0,org.springframework.orm.jpa.SharedEntityManagerCreator#1

最佳答案

由于您有两个实体管理器(来自 EntityMangaerFactory),您需要告诉 spring 您希望 Autowiring 哪个特定的 EntityManager。使用@Qualifier

@Autowired  
@Qualifier("enduserEntityManagerFactory") // use bean id of the Entity Manager Factory you want to inject
EntityManagerFactory entityManagerFactory

EntityManager entityManager = entityManagerFactory.createEntityManager();

关于此的更多信息 here

关于java - 在 spring 应用程序中注入(inject) 2(不久的将来 3)个不同的 entityManagerFactory(或 entityManager),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47415463/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com