问题描述
这应该是一个简单的问题,但我在网上找不到合适的文档.我想这样做:
@MessagingGateway(name = "redemptionGateway", defaultRequestChannel = Channels.GATEWAY_OUTPUT, defaultHeaders = @GatewayHeader(name = "orderId", expression = "#redemption.orderId"))公共接口 RedemptionGateway {无效创建(TrivialRedemption 赎回);}
我显然使用了错误的语句来引用 redemption
的 orderId
成员:
好的.看
@GatewayHeader(name = "orderId", expression = "#redemption.orderId")
此 SpEL 在运行时根据实际参数进行评估,以构建 MessageHeaders
以供目标 Message
发送.
这就是它的样子:
private StandardEvaluationContext createMethodInvocationEvaluationContext(Object[] arguments) {StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);context.setVariable("args", 参数);context.setVariable("gatewayMethod", this.method);返回上下文;}
因此,EvaluationContext
增加了两个变量 args
和 gatewayMethod
.
如您所见,名称中没有任何参数.无论如何,这对所有 JVM 都有效吗?
您可以使用 args
中的参数索引来实现您的目标:
expression = "#args[0].orderId"
This should be a simple question, but I cannot find the proper doc online. I want to do this:
@MessagingGateway(name = "redemptionGateway", defaultRequestChannel = Channels.GATEWAY_OUTPUT, defaultHeaders = @GatewayHeader(name = "orderId", expression = "#redemption.orderId"))
public interface RedemptionGateway {
void create(TrivialRedemption redemption);
}
I am obviously using the wrong statement to reference the orderId
member of redemption
:
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'orderId' cannot be found on null
OK. Look
@GatewayHeader(name = "orderId", expression = "#redemption.orderId")
This SpEL is evaluated at runtime against actual arguments to build MessageHeaders
for target Message
to send.
And that is how it looks:
private StandardEvaluationContext createMethodInvocationEvaluationContext(Object[] arguments) {
StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
context.setVariable("args", arguments);
context.setVariable("gatewayMethod", this.method);
return context;
}
So, EvaluationContext
is enriched with two variables args
and gatewayMethod
.
As you see, there is no any arguments population by their name. Will that work anyway for all the JVMs?
You can achieve your goal using parameter index from the args
:
expression = "#args[0].orderId"
这篇关于如何使用 SPEL 引用@GatewayHeader 中参数的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!