How to use Spring to get Enum value

<bean id="xyz" class="com.abc" > <property name="name"> <bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="com.abc.staticname" /> </bean> </property> </bean> 

This is how I used the class name com.abc before. Names should now appear from another listing. How to access enumeration value to set name property of my class com.abc?

+6
java spring enums
source share
2 answers

I do not understand why you cannot continue to use FieldRetrievingFactoryBean , for what it is needed.

This is a little easier to use than your example suggests. In addition, there is a simpler schema-based syntax that does the same, <util:constant> .

Both approaches are documented (and compared) here .

(Remember that enum values ​​are only static fields in the enum class)

+7
source share

You can simply use the enumeration name as the value, and Spring will automatically detect that it is a static field of the enumeration type and its use. For example, if you have enum com.mycompany.MyEnum with SOMEVAL, ANOTHERVAL values, you can use:

 <property name="myEnumProperty" value="SOMEVAL" /> 

This will allow you to completely exclude FieldRetrievingFactoryBean and <util:constant> .

+2
source share

All Articles