为Entity Manager初始化两次Spring bean

为Entity Manager初始化两次Spring bean,spring,hibernate,jpa,Spring,Hibernate,Jpa,我有一个定义如下的bean <bean id="batchManagementService" class="com.amdocs.dc.sprint.batch.service.BatchManagementServiceImpl" autowire-candidate="false"> <property name="repository" ref="batchPersistenceRepository" /> <property name

我有一个定义如下的bean

 <bean id="batchManagementService" class="com.amdocs.dc.sprint.batch.service.BatchManagementServiceImpl" autowire-candidate="false">
     <property name="repository" ref="batchPersistenceRepository" />
     <property name="timeService" ref="batchTimeService" />     
     <property name="validator" ref="batchValidator" />
  </bean>

我在不同模块的两个地方引用这个bean。 然而,它是用两个不同的值初始化的

  • 使用执行正确数据库事务的JDK代理
  • 直接使用实现器类“BatchManagementServiceImpl”,如上所述。 在第二种情况下,实体管理器返回null,导致所有DB事务失败
  • 实现器类包含事务方法。
    如果您对此有任何帮助,我们将不胜感激。

    对我的描述不太清楚,但听起来您的应用程序上下文在模块中配置错误。 也许您可以尝试以下方法: 单元A: -applicationContextA.xml 模块_B(应该使用模块_A的bean) -applicationContextB.xml 模块C(应使用模块B和模块A) -applicationContextC.xml

    错误的方法是:

    applicationContextA.xml:

    <beans xmlns="http://www.springframework.org/schema/beans">
        ...
        <bean id="batchManagementService"...>
    
    <beans xmlns="http://www.springframework.org/schema/beans">
        ...
        <bean id="batchManagementService"...>
    
    
    ...
    
    applicationContextB.xml:

    <beans xmlns="http://www.springframework.org/schema/beans">
        <import resource="applicationContextA.xml"/>
        <bean id=anotherBean...
    
    <beans xmlns="http://www.springframework.org/schema/beans">
        <!--No import here! -->
        <!--<import resource="applicationContextA.xml"/>-->
        <bean id=anotherBean...
    
    
    
    你能补充更多细节吗?为了更好地帮助你。根据您提供的信息,问题可能在任何地方。您必须在代码中的某个地方调用新的BatchManagementServiceImpl()。
    <beans xmlns="http://www.springframework.org/schema/beans">
        <!--No import here! -->
        <!--<import resource="applicationContextA.xml"/>-->
        <bean id=anotherBean...
    
    <beans xmlns="http://www.springframework.org/schema/beans">
        <!-- No import here -->
        <!--<import resource="applicationContextB.xml"/>-->
        <bean id=anotherBean...
    
    <beans xmlns="http://www.springframework.org/schema/beans">
        <import resource="applicationContextA.xml"/>
        <import resource="applicationContextB.xml"/>
        <import resource="applicationContextC.xml"/>
     </beans>