Shamik Shamik - 6 days ago 7
Java Question

How to use Spring to get the value of an Enum

<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 the way previously I used to set the name of class com.abc. Now, the names should come from another enum. How do I access the enum value to set the name property of my class com.abc?

Answer

I don't see why you can't keep on using FieldRetrievingFactoryBean, that's what it's for.

It's a little bit easier to use than your example suggests, though. Also, there's the easier schema-based syntax which does the same thing, <util:constant>.

Both approaches are documented (and compared) here.

(Remember that enum values are just static fields on the enum class)

Comments