【问题标题】:Defining enum maps in Spring beans在 Spring bean 中定义枚举映射
【发布时间】:2013-02-04 22:47:28
【问题描述】:

我正在尝试在我的 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.

【问题讨论】:

    标签: java spring dictionary dependency-injection


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

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

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

      entry 元素还允许您将对另一个 bean 的引用作为值传递,例如:

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

      (在你的情况下没有用,我只是为了完整性而提到它)

      【讨论】:

        【解决方案3】:

        您是否在标头中定义了这些架构:

        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"
        

        【讨论】:

        • 哎呀,我忘记了,但是当我添加它时,它仍然有错误
        猜你喜欢
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        • 2011-04-14
        • 2017-03-03
        • 1970-01-01
        相关资源
        最近更新 更多