问题
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