spring-integration: MessageProducer may only be referenced once

痴心易碎 提交于 2020-01-05 05:47:09

问题


I want to use a gateway in multiple flows. My gateway definition is:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MarshallingWebServiceOutboundGateway myServiceGateway() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan("blah.*");

    MarshallingWebServiceOutboundGateway gateway = new MarshallingWebServiceOutboundGateway(
            serviceEndpoint, marshaller, messageFactory);
    gateway.setMessageSender(messageSender);
    gateway.setRequestCallback(messageCallback);

    return gateway;
}

Note that I have defined the message gateway bean in scope prototype so that Spring should create multiple gateway instances. Nevertheless I get this message at startup:

Caused by: java.lang.IllegalArgumentException: A reply MessageProducer may only be referenced once (myServiceGateway) - use @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) on @Bean definition.

Why does it insist that a gateway must not be referenced more than once and how can I use the same gateway from multiple flows?

Using spring-integration 5.0.4


回答1:


I thing you have something like .handle(myServiceGateway()) several times.

In this case you have to remove @Bean and @Scope from this method. And it also can be just private. The Java DSL process will create beans for you on the matter. And each flow will have its own instance. As you requested.

Any Spring Integration components can't be @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) at all. They are referenced from non-prototype beans (endpoints) anyway. So, essentially, the scope of your prototype beans are increased.



来源:https://stackoverflow.com/questions/50127547/spring-integration-messageproducer-may-only-be-referenced-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!