2017-03-25 27 views
0

我試圖用一個factory-method初始化service-activator如下Spring集成 - 利用規劃環境地政司在服務激活嵌套豆構造帶參數的

<int:service-activator> 
    <bean class="com.sample.FileWriterFactory" factory-method="createFileWriter"> 
     <constructor-arg index="0" value="${xml.out-directory}/xml"/> 
     <constructor-arg index="1" value="#{ headers['file_name'] + '.xml' }"/> 
    </bean> 
</int:service-activator> 

然而,由於headers屬性未發現規劃環境地政司評估失敗在評估環境中。確切的錯誤片段是

org.springframework.expression.spel.SpelEvaluationException:EL1008E:屬性或字段「頭」不能在類型的對象中找到「org.springframework.beans.factory.config.BeanExpressionContext」 - 也許不公開?

這樣做的目的是通過根據需要傳遞不同的參數來重用相同的POJO。 我在做什麼錯?

回答

0

#{...}表達式在上下文初始化過程中被評估一次。

訪問屬性的表達式(如headers)需要在運行時進行評估(針對消息作爲根對象)。

如果這就是你正在嘗試做的,使用value="headers['file_name'] + '.xml'"然後,你的構造函數中...

private final Expression expression; 

public FileWriterFactory(String directory, String expression) { 
    ... 
    this.expression = new SpelExpressionParser().parseExpression(expression); 
} 

然後,在運行時服務方法

String fileName = this.expression.getValue(message, String.class); 
+0

感謝澄清,@Gary 。像魅力一樣工作! –

相關問題