When calling Spring's "Validate" from Eclipse, I get a lot of errors when I want to get back an enum using Enum's implicit "valueOf" method.
For example:
<bean id="docFamily" class="...DocFamily" factory-method="valueOf">
<constructor-arg>
<value>LOGY</value>
</constructor-arg>
</bean>
has Eclipse telling me:
Non-static factory method 'valueOf' with 1 arguments not found in factory bean class ...
However as I understand it from the documentation:
BeanWrapperImpl supports JDK 1.5 enums and old-style enum classes: String values will be treated as enum value names
So the above should work right? (btw is 'constructor-arg' the correct tag in that case, shouldn't it be some 'method-arg'?).
Why is Eclipse/Spring's "Validate" giving me that error message?
Enum.valueOf()
has two arguments:
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)
Therefore the desired definition may look like this:
<bean id="docFamily" class="java.lang.Enum" factory-method="valueOf">
<constructor-arg index = "0"><value>...DocFamily</value></constructor-arg>
<constructor-arg index = "1"><value>LOGY</value></constructor-arg>
</bean>
However, something like this can be a more elegant solution:
<util:constant id = "docFamily" static-field = "...DocFamily.LOGY" />
I just tried using it like this:
<bean id="docFamily" class="...DocFamily" factory-method="valueOf">
<constructor-arg type="java.lang.String" value="LOGY"/>
</bean>
and it worked like charm. Does it works for you?
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