![](/img/trans.png)
[英]Spring Integration - replace XML by annotation, how to convert anonymous channels?
[英]How to refactor Spring Integration XML to annotation support?
我正在尝试将现有的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" />
但是,如何将服务激活器绑定到错误通道,就像在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
}
}
好吧,既然我是那些新的Java和Annotation配置功能的作者,我可以帮助你。
但你应该做好准备,这样做并不容易。 要完全摆脱xml,你可能会选择我们的另一个新东西 - Java DSL 。
我告诉我们,我们将采取几个步骤使其发挥作用。
gateway.setErrorChannel(MessageChannel);
和@ServiceActivator(inputChannel = "errorChannel")
。 您必须将errorChannel
声明为bean:
@Bean
public MessageChannel errorChannel() {
return new DirectChannel();
}
并从该TCP网关使用它:
gateway.setErrorChannel(this.errorChannel());
或者,如果您依赖于Framework中的默认errorChannel
,您应该@Autowired
它到该Config
。
下一步。 我没有看到@ComponentScan
。 这意味着您的@ServiceActivator
可能对Application上下文不可见。
您应该为TcpInboundGateway
提供更多选项: connectionFactory
, requestChannel
, replyChannel
等。一切都必须是Spring bean。
一开始就足够了。 希望它清楚,对你有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.