Spring 3.2.4 cannot convert String to Enum in <util: map ... / "> although" key-type "is specified
I'm trying to define a map bean since Spring 3.2.4 with Enum as the key type, in this way:
<util:map id="myMapping" key-type="com.acme.MyEnum">
<entry key="ENUM1" value="value1" />
<entry key="ENUM2" value="value2" />
</util:map>
The MyEnum class is a trivial class:
public enum MyEnum
{
ENUM1,
ENUM2
}
When creating the application context, Spring will throw this exception:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myMapping':
Error converting typed String value for bean property 'sourceMap';
nested exception is org.springframework.beans.ConversionNotSupportedException:
Failed to convert value of type 'java.lang.String' to required type
'com.acme.MyEnum'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String] to required type
[com.acme.MyEnum]: no matching editors or conversion strategy found
(formatted for better readability)
I expected Spring to convert the string "ENUM1" to "MyEmum.ENUM1" due to the given key type
key-type="com.acme.MyEnum"
in the bean declaration.
I know how to solve this by doing an alternate bean definition using <entry>
using the fully qualified name of the enum class, etc. But I would like to build the definition as described for readability.
Is this a known bug or misunderstanding on my side?
Many thanks for your help!
source to share
It actually seems to be correct how you are using utils-map, but there might be a problem elsewhere.
I don't know how you would inject the generated myMapping bean. If you are using @Autowired this is the possible reason why you are getting this exception. You should use @Resource(name="myMapping")
instead @Autowired
. Check out this ticket .
I have already tested it and it works. If you want to see how, I created a sample project and pushed to github. Follow this link .
Hope this helps.
source to share