如何在Spring中引用属性中的常量

yoz*_*ozh 16 java spring

我需要指定String常量作为属性的值:

<int:header name="importedFilename" />
Run Code Online (Sandbox Code Playgroud)

这里"importedFilename"不应该是硬编码的,而是取自Constants.IMPORTED_FILENAME_HEADER静态字段中的fe .有办法吗?"int"是Spring Integration命名空间btw.此外,似乎没有适当的bean定义替换int:header- 所以我不能使用<bean class="Header">....

nic*_*ild 23

<util:constant id="importedFilenameHeader"
    static-field="your.package.Constants.IMPORTED_FILENAME_HEADER"/>
Run Code Online (Sandbox Code Playgroud)

然后,您应该能够通过其id(importedFilenameHeader)引用它,以便在您的<int:header>元素中使用,如下所示:

<int:header name="importedFilename" ref="importedFilenameHeader"/>
Run Code Online (Sandbox Code Playgroud)

编辑:

您应该能够使用SpEL执行此操作.它是Spring的表达式语言,它可以在3.0(也可能是2.5)中获得.

我想你可以这样做:

<util:constant id="importedFilenameHeader"
    static-field="your.package.Constants.IMPORTED_FILENAME_HEADER"/>
<int:header name="#{importedFilenameHeader}" ... />
Run Code Online (Sandbox Code Playgroud)

然后Spring应该将它评估为importedFilenameHeader我们在原始部分中定义的Constant的值(这也包含在此示例中).

以下是获取util命名空间的一些位置信息:

<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">
Run Code Online (Sandbox Code Playgroud)


Max*_*asy 10

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

应该工作(见http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html#d0e11977).