java - Spring Integration 和 JPA 更新出站网关时出现循环引用错误

标签 java hibernate spring-boot spring-integration spring-data-jpa

我一直在尝试解决以下 Spring 集成流程中循环引用的问题。

我正在为entityManagerFactory和transactionManager bean使用Spring Boot自动配置。

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

<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 ..> 

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

最佳答案

嗯,我认为我们确实遇到了这个问题:https://jira.spring.io/browse/INT-3857 :CTOR 注入(inject)导致 deps 对于 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 .

关于java - Spring Integration 和 JPA 更新出站网关时出现循环引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327330/

相关文章:

mysql - UTF8 - Hibernate/MySQL 怪异

java - Spring 数据保存与 saveAll 性能

spring-boot - @AllArgsConstructor 和构造函数注入(inject) Spring : is private final needed?

java - 使用 Spring Boot 在 Angular 中成功调用 OPTIONS 后,未调用 GET 请求

java - JPA实体映射到特定表

java - Google Sheet API V4 Java,从列号获取列字母

java - 实体参数的多态 JSON 反序列化 (Jackson) (Google Cloud Endpoints)

Java手动堆栈分配

java - 服务层私有(private)方法

java - Opengl es on android problem with object opsition on projection matrix