如何将Spring Integration XML重构为注释支持?

mem*_*und 5 java xml spring spring-integration

我正在尝试将现有的xml重构Spring Integration为新的4.0.0.注释.

<!-- the service activator is bound to the tcp input gateways error channel -->
<ip:tcp-inbound-gateway error-channel="errorChannel" />
<int:service-activator input-channel="errorChannel" ref="myService" />
Run Code Online (Sandbox Code Playgroud)

但是,如何将服务激活器绑定到错误通道,就像在xml中一样?

@Configuration
@EnableIntegration
public class Config {

    @Bean
    public TcpInboundGateway gate() {
        TcpInboundGateway gateway = new TcpInboundGateway();

        //??? how can I bind the service activator class as it was in xml?
        gateway.setErrorChannel(MessageChannel);
        return gateway;
    }
}


@Service
public class MyService {

    @ServiceActivator(inputChannel = "errorChannel")
    public String send(String data) {
        //business logic
    }
}
Run Code Online (Sandbox Code Playgroud)

Art*_*lan 7

好吧,既然我是那些新的Java和Annotation配置功能的作者,我可以帮助你.

但你应该做好准备,这样做并不容易.要完全摆脱xml,你可能会选择我们的另一个新东西 - Java DSL.

我告诉我们,我们将采取几个步骤使其有效.

gateway.setErrorChannel(MessageChannel);@ServiceActivator(inputChannel = "errorChannel").您必须声明errorChannel为bean:

@Bean
public MessageChannel errorChannel() {
   return new DirectChannel();
}
Run Code Online (Sandbox Code Playgroud)

并从该TCP网关使用它:

gateway.setErrorChannel(this.errorChannel());
Run Code Online (Sandbox Code Playgroud)

或者,如果您依赖于errorChannelFramework中的默认值,那么您应该@Autowired这样做Config.

下一步.我没有看到@ComponentScan.这意味着您@ServiceActivator的应用程序上下文可能不可见.

您应该提供更多的选择TcpInboundGateway反正:connectionFactory,requestChannel,replyChannel等一切必须的Spring bean.

一开始就足够了.希望它清楚,对你有意义.