基於註解的配置
以下示例(來自示例倉庫)展示了在使用註解而非 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);
}
}
| 1 | 標準 Spring Integration 註解,用於啟用整合應用程式的基礎設施。 |
| 2 | 搜尋 @MessagingGateway 介面。 |
| 3 | 流程客戶端的入口點。呼叫應用程式可以使用 @Autowired 注入此 Gateway bean 並呼叫其方法。 |
| 4 | 出站端點由 MessageHandler 和包裝它的消費者組成。在此場景中,@ServiceActivator 根據通道型別配置端點。 |
| 5 | 入站端點(在 TCP/UDP 模組中)都是訊息驅動的,因此只需宣告為簡單的 @Bean 例項即可。 |
| 6 | 此類別提供了一些 POJO 方法,用於此示例流程(伺服器端的 @Transformer 和 @ServiceActivator,以及客戶端的 @Transformer)。 |
| 7 | 客戶端連線工廠。 |
| 8 | 伺服器端連線工廠。 |