开发者

How to reference constant in attribute in Spring

I need to specify String constant as value of attribute:

<int:header name="importedFilename" />

here "i开发者_运维百科mportedFilename" should not be hardcoded but taken from f.e. from Constants.IMPORTED_FILENAME_HEADER static field. Is there a way to do it? "int" is Spring Integration namespace btw. Also it seems that there is no appropriate bean definition to replace int:header with - so I can't use <bean class="Header">....


<util:constant id="importedFilenameHeader"
    static-field="your.package.Constants.IMPORTED_FILENAME_HEADER"/>

You should then be able to reference this by its id (importedFilenameHeader) to be used in your <int:header> element like so:

<int:header name="importedFilename" ref="importedFilenameHeader"/>

EDIT:

You should be able to do this using SpEL. It is Spring's Expression Language, and it is available in 3.0 (maybe 2.5 also?).

I think you can go about doing this then:

<util:constant id="importedFilenameHeader"
    static-field="your.package.Constants.IMPORTED_FILENAME_HEADER"/>
<int:header name="#{importedFilenameHeader}" ... />

Spring should then evaluate this to be the value of the Constant importedFilenameHeader that we defined in the original part (which is also included in this example).

Here is some location information for getting the util namespace:

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-2.5.xsd">


<int:header name="#{T(com.example.Constants).IMPORTED_FILENAME_HEADER}" />

should work (see http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html#d0e11977).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜