从 5.3 版开始,引入了一个允许使用自定义或组合的 EIP 运算符扩展现有 Java DSL。 所需要的只是这个类的扩展,它提供了可以在 Bean 定义中使用的方法。 扩展类也可用于自定义配置;例如,可以在现有扩展中实现 missed 或 default 选项。 下面的示例演示了复合自定义运算符和默认自定义扩展的用法:IntegrationFlowExtensionIntegrationFlowIntegrationComponentSpecIntegrationComponentSpecAggregatorSpecoutputProcessorSpring中文文档

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 运算符必须返回扩展类。 这样,目标定义将与新的和现有的 DSL 运算符一起使用:IntegrationFlowSpring中文文档

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