spring-integration: MessageProducer may only be re

2019-08-17 12:11发布

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条回答
▲ chillily
2楼-- · 2019-08-17 13:16

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.

查看更多
登录 后发表回答