【问题标题】:Circular Reference Error with Spring Integration and JPA updating outbound gatewaySpring 集成和 JPA 更新出站网关的循环引用错误
【发布时间】:2015-12-17 05:36:03
【问题描述】:

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

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

这是具有两个 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,应用程序运行良好。任何 cmets 或建议将不胜感激。

【问题讨论】:

  • 您只需要删除 1 个网关吗?或两者?你能发布一个演示问题的小应用程序(例如在 github 上)吗?
  • 我只需要删除一个。删除任何一个网关,应用程序运行正常。
  • 哪一个重要吗?
  • 我想知道 errorMessageUpdatingGateway 是什么...我们在您的配置中没有看到它的定义。
  • 我已经能够在本地复制它。奇怪的情况:当我们有多个 entity-manager-factory="entityManagerFactory" 使用时,我们会失败。正在调查...

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


【解决方案1】:

好吧,我认为我们确实有这个问题:https://jira.spring.io/browse/INT-3857:CTOR 注入导致 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

【讨论】:

  • 是的,带有 XML 配置的 jpaOperations bean 工作正常,但 Java 配置抛出了同样的错误。
  • Java 配置?这里有什么新东西吗?请分享!
  • 使用@Bean public JpaOperations jpaOperations(EntityManagerFactory entityManagerFactory){ DefaultJpaOperations jpaOperations = new DefaultJpaOperations(); jpaOperations.setEntityManagerFactory(entityManagerFactory);返回 jpa 操作; } 没有用,但你的答案中的 xml 有效。
  • 正确,因为 JpaOutboundGatewayFactoryBean 使用 CTOR 注入,必须修复。由于您已经使用了一些 XML,我假设您可以使用更简单的 &lt;bean&gt; 直到我们修复:-)
  • :) 是的,我将使用 xml。我仅将 xml 用于集成流程,因为它更易于使用图表进行可视化,否则我更喜欢 Java Config 用于我的 bean 配置。再次感谢您的解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多