<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?
To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.
Select element using Enum As you can call any java methods within thymeleaf expressions, It is easier to get all the enum values for Role using the Role. values() method as shown below. As the values() method return an array, It is really easy to iterate through then using th:each as shown below.
The Java compiler internally adds the values() method when it creates an enum. The values() method returns an array containing all the values of the enum.
valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With