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

DSL 扩展

从 5.3 版开始,IntegrationFlowExtension已引入以允许使用自定义或组合的 EIP 运算符扩展现有的 Java DSL。 所需要的只是该类的扩展,它提供了可在IntegrationFlowbean 定义。 扩展类也可用于自定义IntegrationComponentSpec配置;例如,可以在现有的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();
}