通过XML注入Entitymanager,而不是注释

前端之家收集整理的这篇文章主要介绍了通过XML注入Entitymanager,而不是注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要做的是通过 XML注入几乎与通过A @PersistenceContext注释完成的方法相同.我需要这个,因为我有不同的实体经理我需要注入到同一个DAO.数据库彼此镜像,我宁愿拥有一个基类,对于该基类的实例,然后创建多个类,以便我可以使用@PersistenceContext注释.

这是我的例子.这是我现在在做的,它的作品.

  1. public class ItemDaoImpl {
  2. protected EntityManager entityManager;
  3.  
  4. public List<Item> getItems() {
  5. Query query = entityManager.createQuery("select i from Item i");
  6. List<Item> s = (List<Item>)query.getResultList();
  7. return s;
  8. }
  9. public void setEntityManger(EntityManager entityManager) {
  10. this.entityManager = entityManager;
  11. }
  12. }
  13.  
  14. @Repository(value = "itemDaoStore2")
  15. public class ItemDaoImplStore2 extends ItemDaoImpl {
  16.  
  17. @PersistenceContext(unitName = "persistence_unit_2")
  18. public void setEntityManger(EntityManager entityManager) {
  19. this.entityManager = entityManager;
  20. }
  21. }
  22.  
  23. @Repository(value = "itemDaoStore1")
  24. public class ItemDaoImplStore1 extends ItemDaoImpl {
  25.  
  26. @PersistenceContext(unitName = "persistence_unit_1")
  27. public void setEntityManger(EntityManager entityManager) {
  28. this.entityManager = entityManager;
  29. }
  30. }

事务管理器,实体管理器定义如下…

  1. <!-- Registers Spring's standard post-processors for annotation-based configuration like @Repository -->
  2. <context:annotation-config />
  3.  
  4. <!-- For @Transactional annotations -->
  5. <tx:annotation-driven transaction-manager="transactionManager1" />
  6. <tx:annotation-driven transaction-manager="transactionManager2" />
  7.  
  8. <!-- This makes Spring perform @PersistenceContext/@PersitenceUnit injection: -->
  9. <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
  10.  
  11. <!-- Drives transactions using local JPA APIs -->
  12. <bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
  13. <property name="entityManagerFactory" ref="entityManagerFactory1" />
  14. </bean>
  15.  
  16. <bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
  17. <property name="entityManagerFactory" ref="entityManagerFactory2" />
  18. </bean>
  19.  
  20. <bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean">
  21. <property name="persistenceUnitName" value="persistence_unit_1"/>
  22. ...
  23. </bean>
  24.  
  25. <bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean">
  26. <property name="persistenceUnitName" value="persistence_unit_2"/>
  27. ...
  28. </bean>

我想做的是不创建类ItemDaoImplStore2或ItemDaoImplStore1.我想通过xml将这些作为ItemDaoImpl的实例.我不知道如何注入实体管理员.我想要模拟这个注释为“Repository”注解,我也希望能够通过持久化单元名称来指定要注入的entityManager.我想用类似于下面的东西来使用XML.

  1. <!-- Somehow annotate this instance as a @Repository annotation -->
  2. <bean id="itemDaoStore1" class="ItemDaoImpl">
  3. <!-- Does not work since it is a LocalContainerEntityManagerfactorybean-->
  4. <!-- Also I would perfer to do it the same way PersistenceContext works
  5. and only provide the persistence unit name. I would like to be
  6. able to specify persistence_unit_1-->
  7. <property name="entityManager" ref="entityManagerFactory1"/>
  8. </bean>
  9.  
  10. <!-- Somehow annotate this instance as a @Repository annotation -->
  11. <bean id="itemDaoStore2" class="ItemDaoImpl">
  12. <!-- Does not work since it is a LocalContainerEntityManagerfactorybean-->
  13. <!-- Also I would perfer to do it the same way PersistenceContext works
  14. and only provide the persistence unit name. I would like to be
  15. able to specify persistence_unit_2-->
  16. <property name="entityManager" ref="entityManagerFactory2"/>
  17. </bean>
使用SharedEntityManagerBean – 它为EntityManagerFactory创建一个共享的EntityManager,与@PersistenceContext相同:
  1. <bean id="itemDaoStore1" class="ItemDaoImpl">
  2. <property name="entityManager">
  3. <bean class = "org.springframework.orm.jpa.support.SharedEntityManagerBean">
  4. <property name = "entityManagerFactory" ref="entityManagerFactory1"/>
  5. </bean>
  6. </property>
  7. </bean>

猜你在找的XML相关文章