Search code examples
javaspringspring-el

How to reference a constant in Spring Expression Language


i am quite new to Spring and i have a bean declaration as follows.

    <bean id="mybean" class="" scope="prototype">
       <property name='typeOf' value='#{typeOfBuilder.getKeyFor("OPEN_DATE").getId()}'/>    
</bean> 

typeOf is a type of Integer which is the key of another table which typeOfBuilder builds by Key which is OPEN_DATE in this case.

this code works OK but have a limitation. OPEN_DATE is a constant in a NON-MANAGE Spring Bean something like follows.

public final class Constants
{
     public final static String KEY_FOR_OPEN_DATE = "OPEN_DATE";     
} 

and is strongly recommend to be able to reference to it!!.

something like this.

<util:constant id="PATH_TO_CONSTANT" static-field="myPath"/>
<property name='typeOf' value='#{typeOfBuilder.getKeyFor(PATH_TO_CONSTANT).getId()}'/>  

any help is hugely appreciate.


Solution

  • The special 'T' operator can be used to specify an instance of java.lang.Class (the 'type'). Static methods are invoked using this operator as well.

    Try the code below.

    <property name='typeOf' value='#{typeOfBuilder.getKeyFor(T(some.package.Constants).KEY_FOR_OPEN_DATE).getId()}'/>