I'm trying to define a map bean with Spring 3.2.4 with an Enum as the key type, 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 throws 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 readbility)


I expected Spring to convert the String "ENUM1" to "MyEmum.ENUM1", because of the given key type



key-type="com.acme.MyEnum"


in the mapping bean declaration.


I know how to solve this by doing an alternate bean definition, using <entry>, using the full qualified class name of the enum etc... But I would like to build the defintion as described for easy readability.


Is this a known bug or a lack of understanding on my side?


Thanks a lot for your help!


No comments:

Post a Comment