我试图通过XML方式注入实体管理器(entity manager),这个过程与 @PersistenceContext 注解的用法几乎一致。我需要这样做是因为我需要将不同的实体管理器注入到同一个 DAO 中。这些数据库相互镜像,所以我宁愿只有一个基类并创建该基类的多个实例,而不是创建多个类来使用 @PersistenceContext 注解。
以下是我的示例。目前我正在使用下面的方法并已经能够正常工作。
以下是我的示例。目前我正在使用下面的方法并已经能够正常工作。
public class ItemDaoImpl {
protected EntityManager entityManager;
public List<Item> getItems() {
Query query = entityManager.createQuery("select i from Item i");
List<Item> s = (List<Item>)query.getResultList();
return s;
}
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore2")
public class ItemDaoImplStore2 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_2")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
@Repository(value = "itemDaoStore1")
public class ItemDaoImplStore1 extends ItemDaoImpl {
@PersistenceContext(unitName = "persistence_unit_1")
public void setEntityManger(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
下面定义了 TransactionManagers 和 EntityManagers...
<!-- Registers Spring's standard post-processors for annotation-based configuration like @Repository -->
<context:annotation-config />
<!-- For @Transactional annotations -->
<tx:annotation-driven transaction-manager="transactionManager1" />
<tx:annotation-driven transaction-manager="transactionManager2" />
<!-- This makes Spring perform @PersistenceContext/@PersitenceUnit injection: -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<!-- Drives transactions using local JPA APIs -->
<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>
<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_1"/>
...
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistence_unit_2"/>
...
</bean>
我想要做的是不创建ItemDaoImplStore2或ItemDaoImplStore1类,而是通过xml将它们作为ItemDaoImpl的实例。然而,我不知道如何正确注入entitymanager。我想模拟将其注释为'Repository'注释,并且我也想能够通过持久性单位名称指定要注入的entityManager。我希望使用XML实现类似于下面的内容。
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore1" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_1-->
<property name="entityManager" ref="entityManagerFactory1"/>
</bean>
<!-- Somehow annotate this instance as a @Repository annotation -->
<bean id="itemDaoStore2" class="ItemDaoImpl">
<!-- Does not work since it is a LocalContainerEntityManagerFactoryBean-->
<!-- Also I would perfer to do it the same way PersistenceContext works
and only provide the persistence unit name. I would like to be
able to specify persistence_unit_2-->
<property name="entityManager" ref="entityManagerFactory2"/>
</bean>