DSL and Endpoint Configuration

所有 IntegrationFlowBuilder EIP 方法都具有一个变量,它应用 lambda 参数以提供 AbstractEndpoint 实例的选项:SmartLifecyclePollerMetadatarequest-handler-advice-chain 等。每个变量都有泛型参数,因此它们允许你在上下文中配置一个端点甚至其 MessageHandler,如下例所示:

All IntegrationFlowBuilder EIP methods have a variant that applies the lambda parameter to provide options for AbstractEndpoint instances: SmartLifecycle, PollerMetadata, request-handler-advice-chain, and others. Each of them has generic arguments, so it lets you configure an endpoint and even its MessageHandler in the context, as the following example shows:

@Bean
public IntegrationFlow flow2() {
    return IntegrationFlow.from(this.inputChannel)
                .transformWith(t -> t
                              .transformer(new PayloadSerializingTransformer())
                              .autoStartup(false)
                              .id("payloadSerializingTransformer"))
                .transformWith(t -> t
                              .transformer((Integer p) -> p * 2)
                              .advice(expressionAdvice()))
                .get();
}

另外,EndpointSpec 提供了一个 id() 方法,可用于使用给定的 Bean 名称(而不是生成名称)注册端点 Bean。

In addition, the EndpointSpec provides an id() method to let you register an endpoint bean with a given bean name, rather than a generated one.

如果 MessageHandler 被作为 Bean 引用,那么当 .advice() 方法在 DSL 定义中出现时,任何现有的 adviceChain 配置都将被覆盖:

If the MessageHandler is referenced as a bean, then any existing adviceChain configuration will be overridden if the .advice() method is present in the DSL definition:

@Bean
public TcpOutboundGateway tcpOut() {
    TcpOutboundGateway gateway = new TcpOutboundGateway();
    gateway.setConnectionFactory(cf());
    gateway.setAdviceChain(Collections.singletonList(fooAdvice()));
    return gateway;
}

@Bean
public IntegrationFlow clientTcpFlow() {
    return f -> f
        .handle(tcpOut(), e -> e.advice(testAdvice()))
        .transform(Transformers.objectToString());
}

它们不会被合并,这种情况下仅 testAdvice() Bean 被使用。

They are not merged, only the testAdvice() bean is used in this case.