Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference constant in attribute in Spring

Tags:

java

spring

I need to specify String constant as value of attribute:

<int:header name="importedFilename" />

here "importedFilename" 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">....

like image 374
yozh Avatar asked Sep 05 '11 15:09

yozh


2 Answers

<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">
like image 117
nicholas.hauschild Avatar answered Nov 06 '22 16:11

nicholas.hauschild


<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).

like image 27
Max Nanasy Avatar answered Nov 06 '22 16:11

Max Nanasy