Spring 集成和 JPA 更新出站网关时出现循环引用错误

Circular Reference Error with Spring Integration and JPA updating outbound gateway

我一直在尝试找出以下 spring 集成流程中循环引用的问题。

我正在使用 Spring entityManagerFactory 和 transactionManager bean 的引导自动配置。

这是具有两个 jpa:updating-outbound-gateway 的集成上下文,它从两个不同的通道读取消息并将响应发送到一个公共通道。

<int-jms:message-driven-channel-adapter
        id="jmsListener" connection-factory="connectionFactory"
        channel="queueChannel" destination="queueName"
        error-channel="errorChannel"/>

<int:chain input-channel="queueChannel" output-channel="dbChannel">
    ....
    // more stuff
    ....
</int:chain>

<int-jpa:updating-outbound-gateway id="updatingGateway1" request-channel="dbChannel" entity-manager-factory="entityManagerFactory" entity-class="com.example.MyMessage1" persist-mode="PERSIST" reply-channel="reportChannel" reply-timeout="5000">
    <int-jpa:transactional transaction-manager="transactionManager" propagation="REQUIRED"/>
</int-jpa:updating-outbound-gateway> 

<int:channel id="errorChannel"/>

<int-jpa:updating-outbound-gateway id="updatingGateway2" request-channel="errorChannel" entity-manager-factory="entityManagerFactory" entity-class="com.example.MyMessage2" persist-mode="PERSIST" reply-channel="reportChannel" reply-timeout="5000">
    <int-jpa:transactional transaction-manager="transactionManager" propagation="REQUIRED"/>
</int-jpa:updating-outbound-gateway>

<int:channel id="reportChannel"/>
<int:logging-channel-adapter channel="reportChannel" expression="payload"/>

我在 运行 应用程序时遇到以下错误。

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSourceInitializerPostProcessor': 
Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not 
autowire field: private org.springframework.beans.factory.BeanFactory 

org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor.beanFactory; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.integration.jpa.outbound.JpaOutboundGatewayFactoryBean#0': 
Cannot resolve reference to bean 'updatingGateway2.jpaExecutor' 
while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'updatingGateway2.jpaExecutor': 
Cannot resolve reference to bean 'entityManagerFactory' 
while setting constructor argument; nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': 
Injection of autowired dependencies failed; nested exception is 
org.springframework.beans.factory.BeanCreationException: Could not 
autowire field: private javax.sql.DataSource 
org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; 
nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 
'org.springframework.integration.jpa.outbound.JpaOutboundGatewayFactoryBean#1': 
Cannot resolve reference to bean 'updatingGateway1.jpaExecutor' while 
setting constructor argument; nested exception is 
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'updatingGateway1.jpaExecutor': 
Cannot resolve reference to bean 'entityManagerFactory' while setting  
constructor argument; nested exception is 
org.springframework.beans.factory.BeanCurrentlyInCreationException: 
Error creating bean with name 'entityManagerFactory': Requested bean 
is currently in creation: Is there an unresolvable circular reference?

但是,如果我删除其中一个

<int-jpa:updating-outbound-gateway ..> 

flow,应用程序运行良好。如有任何意见或建议,我们将不胜感激。

好吧,我想我们确实遇到了这个问题:https://jira.spring.io/browse/INT-3857:CTOR 注入导致 dep 过早实例化 FactoryBean

很遗憾,我们刚刚发布了 SI-4.2.4,因此 JpaOutboundGatewayFactoryBean 的修复将在稍后的某个时间进行。

同时,您可以尝试将其用作解决方法吗?

<bean id="jpaOperations" class="org.springframework.integration.jpa.core.DefaultJpaOperations">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<int-jpa:updating-outbound-gateway jpa-operations="jpaOperations" />

而不是 entity-manager-factory="entityManagerFactory"

关于此事的 JIRA 票证:https://jira.spring.io/browse/INT-3916