我一直在尝试使用Spring定义一个EnumMap.我尝试了以下变化
<util:map map-class="java.util.EnumMap" key-type="xyz.EnumType">
<entry key="SOME_ENUM_TYPE">
<ref bean="someBean"/>
</entry>
</util:map>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Error creating bean with name 'util:map#1c599b0e': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.EnumMap]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.util.EnumMap.<init>()
Run Code Online (Sandbox Code Playgroud)
以下定义是我最初尝试过的
<util:map map-class="java.util.EnumMap">
<entry key="SOME_ENUM_TYPE">
<ref bean="someBean"/>
</entry>
</util:map>
Run Code Online (Sandbox Code Playgroud)
这给了我一些错误,无法将enumtype分配给String.
网站上有一些使用通用地图的例子,但我试图看看我是否可以使用EnumMap,因为它被认为是Enums的最佳选择.答案可能非常明显,所以如果问题很愚蠢我会道歉.这可能是由于我对Spring的了解有限.谢谢
axt*_*avt 13
我猜你不能初始化EnumMap用<util:map>.但是,EnumMap有一个带现有的构造函数Map,你可以尝试使用它:
<bean class = "java.util.EnumMap">
<constructor-arg>
<util:map key-type="xyz.EnumType">
<entry key="SOME_ENUM_TYPE"><ref bean="someBean"/></entry>
</util:map>
</constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3804 次 |
| 最近记录: |