2016-08-12 19 views
1

這應該是一個簡單的問題,但我無法在線找到正確的文檔。我想這樣做:如何使用SPEL引用@GatewayHeader中的參數屬性

@MessagingGateway(name = "redemptionGateway", defaultRequestChannel = Channels.GATEWAY_OUTPUT, defaultHeaders = @GatewayHeader(name = "orderId", expression = "#redemption.orderId")) 
public interface RedemptionGateway { 
    void create(TrivialRedemption redemption); 
} 

我明明使用了錯誤的語句引用的redemptionorderId成員:

 
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'orderId' cannot be found on null 

回答

2

確定。看

@GatewayHeader(name = "orderId", expression = "#redemption.orderId") 

該規劃環境地政司在對實際參數運行評估,以建立MessageHeaders爲目標Message發送。

這是它的外觀:

private StandardEvaluationContext createMethodInvocationEvaluationContext(Object[] arguments) { 
    StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory); 
    context.setVariable("args", arguments); 

    context.setVariable("gatewayMethod", this.method); 
    return context; 
} 

所以,EvaluationContext富含兩個變量argsgatewayMethod

正如你所看到的,他們的名字沒有任何爭論人口。無論如何,這對於所有的JVM是否會起作用?

可以使用參數指數從args實現你的目標:

expression = "#args[0].orderId" 
+0

偉大的答案! –