在Spring beans中定义枚举映射

3

我正在尝试在Spring beans 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>

更新

这是我的Bean配置:

<?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.
3个回答

5
您是否在头部定义了这些模式:
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"

哎呀,我忘记了,但是当我添加它时,它仍然有错误。 - user962206

1
你可以这样定义它:

    <bean id="springBean" class="a.b.c.d.MyEnum">
        <constructor-arg>
            <util:map key-type="a.b.c.d.MyEnum" value-type="aa.b.c.d.Mya.b.c.d.MyVal">
                <entry>
                    <key><value type="a.b.c.d.MyEnum">ENUM-VAL1</value></key>
                    <ref bean="myValBean1" />
                </entry>
                <entry>
                    <key><value type="a.b.c.d.MyEnum">ENUM-VAL1</value></key>
                    <ref bean="myValBean1" />
                </entry>
            </util:map>
        </constructor-arg>
    </bean>

0

这个错误信息意味着entry元素必须为空,即不包含任何文本或其他元素。您需要的语法是:

<entry key="stockitem.SOAP" value="100"/>
entry元素还允许您将另一个bean的引用作为值传递,例如:
<entry key="stockitem.SOAP" value-ref="myOtherBean"/>

(这在您的情况下没有用处,我只是为了完整性而提及)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接