2013-02-20 200 views
3

我想在我的Spring bean XML来定义一个枚举映射,我希望它在XML填充,但是当我尝试定义它像这样定义枚举地图

<bean class = "java.util.EnumMap"> 
    <constructor-arg> 
     <util:map key-type="org.itemlist.products.stockitem"> 
      <entry key="stockitem.SOAP">100</entry> 
     </util:map> 
    </constructor-arg> 

UPDATE

这里是我的豆子配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 


    <bean class = "java.util.EnumMap"> 
     <constructor-arg> 
      <util:map key-type="org.itemlist.products.stockitem"> 
       <entry key="stockitem.SOAP">100</entry> 
      </util:map> 
     </constructor-arg> 
    </bean> 

</beans> 

当我里面添加条目的价值,这就是现在的错误

cvc-complex-type.2.3: Element 'entry' cannot have character [children], because the type's content type is element-only. 

回答

4

有你定义在标题的模式:

http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd 

而命名空间:

xmlns:util="http://www.springframework.org/schema/util" 
+0

Ooops,我忘记了,但是当我添加它仍然有一个错误 – user962206 2013-02-20 02:34:48

+0

听起来像你应该更新的东西,告诉我们什么是新问题... – 2013-02-20 02:37:04

+0

我已经更新它。 – user962206 2013-02-20 02:41:00

0

该错误消息意味着entry元素必须是空的,即不包含任何文字或其他元素。你想要的语法是:

<entry key="stockitem.SOAP" value="100"/> 

entry元素也可以传递一个引用另一个bean的值,例如:

<entry key="stockitem.SOAP" value-ref="myOtherBean"/> 

(这是没有用的,你的情况,我只是提到它的完整性)