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

我想要做的是通过 XML注入几乎与通过A @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;
 }
}

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

<!-- 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的实例.我不知道如何注入实体管理员.我想要模拟这个注释为“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>
使用SharedEntityManagerBean – 它为EntityManagerFactory创建一个共享的EntityManager,与@PersistenceContext相同:
<bean id="itemDaoStore1" class="ItemDaoImpl"> 
    <property name="entityManager">
        <bean class = "org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name = "entityManagerFactory" ref="entityManagerFactory1"/>  
        </bean>
    </property>
</bean>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇