Annotation-Based Configuration

示例存储库中的以下示例展示了当您使用注解而不是 XML 时可用的某些配置选项:

The following example from the samples repository shows some of the configuration options available when you use annotations instead of 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 Standard Spring Integration annotation enabling the infrastructure for an integration application.
3 Searches for @MessagingGateway interfaces.
4 流程客户端的入口点。调用应用程序可以使用此 `Gateway`bean 的 `@Autowired`并调用其方法。
5 The entry point to the client-side of the flow. The calling application can use @Autowired for this Gateway bean and invoke its method.
6 出站端点包含一个 MessageHandler`和一个将其包装起来的消费者。在该情形中,@ServiceActivator`会根据通道类型配置端点。
7 Outbound endpoints consist of a MessageHandler and a consumer that wraps it. In this scenario, the @ServiceActivator configures the endpoint, according to the channel type.
8 入站端点(在 TCP/UDP 模块中)全部是消息驱动的,因此仅需声明为简单的 `@Bean`实例。
9 Inbound endpoints (in the TCP/UDP module) are all message-driven and so only need to be declared as simple @Bean instances.
10 本类提供许多 POJO 方法用于此示例流程(服务器端的 @Transformer`和 `@ServiceActivator,以及客户端的 @Transformer)。
11 This class provides a number of POJO methods for use in this sample flow (a @Transformer and @ServiceActivator on the server side and a @Transformer on the client side).
12 The client-side connection factory.
13 The server-side connection factory.