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

服务激活器和.handle()方法

.handle()EIP方法的目标是调用任意消息处理器在某个POJO上实现或任何方法。 另一种选择是通过λ表达式定义一个“活动”。 因此,我们引入了一种通用药通用处理<P>功能性接口。 其处理方法需要两个参数:P有效载荷MessageHeaders 头部(从版本5.1开始)。 有了这个条件,我们可以定义一个流如下:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow myFlow() {
    return IntegrationFlow.from("flow3Input")
        .<Integer>handle((p, h) -> p * 2)
        .get();
}

前例会使其接收到的任意整数加倍。spring-doc.cadn.net.cn

然而,春季集成的一个主要目标是松耦合通过运行时类型转换,将消息有效载荷转换为消息处理器的目标参数。 由于 Java 不支持对 lambda 类的通用类型解析,我们引入了一个额外的变通方法有效载荷类型关于大多数EIP方法的论证LambdaMessage处理器. 这样做将艰难的改造工作委托给了斯普林转换服务,该 使用 所提供的类型以及请求给目标方法参数的消息。 以下示例展示了得到的集成流程可能看起来像:spring-doc.cadn.net.cn

@Bean
public IntegrationFlow integerFlow() {
    return IntegrationFlow.from("input")
            .<byte[], String>transform(p - > new String(p, "UTF-8"))
            .handle(Integer.class, (p, h) -> p * 2)
            .get();
}

我们也可以注册一些BytesToInteger转换器转换服务去掉那个额外的.transform():spring-doc.cadn.net.cn

@Bean
@IntegrationConverter
public BytesToIntegerConverter bytesToIntegerConverter() {
   return new BytesToIntegerConverter();
}

@Bean
public IntegrationFlow integerFlow() {
    return IntegrationFlow.from("input")
             .handle(Integer.class, (p, h) -> p * 2)
            .get();
}