对于最新的稳定版本,请使用 Spring Integration 6.4.3spring-doc.cadn.net.cn

DSL 扩展

从版本 5.3 开始,IntegrationFlowExtension以允许使用自定义或组合的 EIP 运算符扩展现有 Java DSL。 所需要的只是这个类的扩展,它提供可以在IntegrationFlowbean 定义。 扩展类也可以用于自定义IntegrationComponentSpec配置;例如,missed 或 default 选项可以在现有的IntegrationComponentSpec外延。 下面的示例演示了复合自定义运算符和AggregatorSpec默认自定义outputProcessor:spring-doc.cadn.net.cn

public class CustomIntegrationFlowDefinition
        extends IntegrationFlowExtension<CustomIntegrationFlowDefinition> {

    public CustomIntegrationFlowDefinition upperCaseAfterSplit() {
        return split()
                .transform("payload.toUpperCase()");
    }

    public CustomIntegrationFlowDefinition customAggregate(Consumer<CustomAggregatorSpec> aggregator) {
        return register(new CustomAggregatorSpec(), aggregator);
    }

}

public class CustomAggregatorSpec extends AggregatorSpec {

    CustomAggregatorSpec() {
        outputProcessor(group ->
                group.getMessages()
                        .stream()
                        .map(Message::getPayload)
                        .map(String.class::cast)
                        .collect(Collectors.joining(", ")));
    }

}

对于方法链流,这些扩展中的新 DSL 运算符必须返回扩展类。 这样,目标IntegrationFlowdefinition 将与新的和现有的 DSL 运算符一起使用:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow customFlowDefinition() {
    return
            new CustomIntegrationFlowDefinition()
                    .log()
                    .upperCaseAfterSplit()
                    .channel("innerChannel")
                    .customAggregate(customAggregatorSpec ->
                            customAggregatorSpec.expireGroupsUponCompletion(true))
                    .logAndReply();
}