DSL 和端點配置

所有 IntegrationFlowBuilder EIP 方法都有一個變體,該變體將 lambda 參數應用於為 AbstractEndpoint 實例提供選項:SmartLifecyclePollerMetadatarequest-handler-advice-chain 等。它們每個都有泛型參數,因此它讓您可以在上下文中配置端點,甚至配置其 MessageHandler,如下列範例所示

@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,而不是產生一個。

如果 MessageHandler 作為 bean 引用,那麼如果 .advice() 方法存在於 DSL 定義中,則任何現有的 adviceChain 配置都將被覆寫

@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。