我一直在尝试解决以下 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/