EIP 方法的目标是在某些 POJO 上调用任何实现或任何方法。
另一种选择是使用 lambda 表达式定义“活动”。
因此,我们引入了一个通用的功能接口。
它的方法需要两个参数:和(从版本 5.1 开始)。
有了这个,我们可以定义一个流,如下所示:.handle()
MessageHandler
GenericHandler<P>
handle
P payload
MessageHeaders headers
@Bean
public IntegrationFlow myFlow() {
return IntegrationFlow.from("flow3Input")
.<Integer>handle((p, h) -> p * 2)
.get();
}
前面的示例将它接收到的任何整数加倍。
但是,Spring Integration 的一个主要目标是通过从消息有效负载到消息处理程序的目标参数的运行时类型转换。
由于 Java 不支持 lambda 类的泛型类型解析,因此我们引入了一种解决方法,其中包含一个针对大多数 EIP 方法的附加参数和 .
这样做会将硬转换工作委托给 Spring 的,它使用提供的和请求的消息来定位方法参数。
以下示例显示了结果可能如下所示:loose coupling
payloadType
LambdaMessageProcessor
ConversionService
type
IntegrationFlow
@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();
}
我们也可以在其中注册一些以摆脱额外的:BytesToIntegerConverter
ConversionService
.transform()
@Bean
@IntegrationConverter
public BytesToIntegerConverter bytesToIntegerConverter() {
return new BytesToIntegerConverter();
}
@Bean
public IntegrationFlow integerFlow() {
return IntegrationFlow.from("input")
.handle(Integer.class, (p, h) -> p * 2)
.get();
}
另请参阅 Lambdas 和 Message<?>
参数。