加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:41:57 所属栏目:百科 来源:网络整理
导读:我想要做的是通过 XML注入几乎与通过A @PersistenceContext注释完成的方法相同.我需要这个,因为我有不同的实体经理我需要注入到同一个DAO.数据库彼此镜像,我宁愿拥有一个基类,对于该基类的实例,然后创建多个类,以便我可以使用@PersistenceContext注释. 这是
我想要做的是通过 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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读