【发布时间】:2015-01-12 20:37:21
【问题描述】:
我正在尝试使用 Spring 3.2.4 定义一个映射 bean,其中 Enum 作为键类型,这样:
<util:map id="myMapping" key-type="com.acme.MyEnum">
<entry key="ENUM1" value="value1" />
<entry key="ENUM2" value="value2" />
</util:map>
MyEnum 类是一个普通的类:
public enum MyEnum
{
ENUM1,
ENUM2
}
在创建应用上下文时,Spring 抛出这个异常:
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
(为了更好的可读性而格式化)
由于给定的键类型,我希望 Spring 将字符串“ENUM1”转换为“MyEmum.ENUM1”
key-type="com.acme.MyEnum"
在映射 bean 声明中。
我知道如何通过使用<entry>、使用枚举的全限定类名等来做一个备用 bean 定义来解决这个问题......但我想按照描述的方式构建定义以便于阅读。
这是一个已知的错误还是我缺乏理解?
非常感谢您的帮助!
【问题讨论】: