基于注解的配置
以下是来自示例仓库的示例,展示了当你使用注解而不是 XML 时可用的一些配置选项:
@EnableIntegration 1
@IntegrationComponentScan 2
@Configuration
public static class Config {
@Value(${some.port})
private int port;
@MessagingGateway(defaultRequestChannel="toTcp") 3
public interface Gateway {
String viaTcp(String in);
}
@Bean
@ServiceActivator(inputChannel="toTcp") 4
public MessageHandler tcpOutGate(AbstractClientConnectionFactory connectionFactory) {
TcpOutboundGateway gate = new TcpOutboundGateway();
gate.setConnectionFactory(connectionFactory);
gate.setOutputChannelName("resultToString");
return gate;
}
@Bean 5
public TcpInboundGateway tcpInGate(AbstractServerConnectionFactory connectionFactory) {
TcpInboundGateway inGate = new TcpInboundGateway();
inGate.setConnectionFactory(connectionFactory);
inGate.setRequestChannel(fromTcp());
return inGate;
}
@Bean
public MessageChannel fromTcp() {
return new DirectChannel();
}
@MessageEndpoint
public static class Echo { 6
@Transformer(inputChannel="fromTcp", outputChannel="toEcho")
public String convert(byte[] bytes) {
return new String(bytes);
}
@ServiceActivator(inputChannel="toEcho")
public String upCase(String in) {
return in.toUpperCase();
}
@Transformer(inputChannel="resultToString")
public String convertResult(byte[] bytes) {
return new String(bytes);
}
}
@Bean
public AbstractClientConnectionFactory clientCF() { 7
return new TcpNetClientConnectionFactory("localhost", this.port);
}
@Bean
public AbstractServerConnectionFactory serverCF() { 8
return new TcpNetServerConnectionFactory(this.port);
}
}
标准的 Spring Integration 注解,启用集成应用程序的基础设施。
搜索带有
@MessagingGateway
的接口。流的客户端入口点。调用应用程序可以使用
@Autowired
来注入这个Gateway
bean 并调用它的方法。外发端点由一个
MessageHandler
和一个包装它的消费者组成。在此场景中,@ServiceActivator
根据通道类型配置端点。入站端点(在 TCP/UDP 模块中)都是消息驱动的,因此只需要声明为简单的
@Bean
实例。该类为此示例流提供了多个 POJO 方法(服务器端的
@Transformer
和@ServiceActivator
以及客户端的@Transformer
)。客户端连接工厂。
服务器端连接工厂。