【问题标题】:Spring Map<Object, Object> InjectionSpring Map<Object, Object> 注入
【发布时间】:2012-05-17 20:05:55
【问题描述】:

我一直在尝试以下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
................
>


<bean id="dateBean" class="org.apache.velocity.tools.generic.NumberTool"/>

<bean id="service" class="package.xyz">
    <property name="toolMap">
    <util:map map-class="java.util.HashMap">
        <entry key="number"><ref:bean="dateBean"/></entry>
    </util:map>
    </property>
</bean>

如您所见,这是用于 Velocity 工具配置的。

我得到了这个例外:

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'util:map'.

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be   found for element 'util:map'.

谁能帮我解决这个问题?

更新:我添加了 XML 声明。

【问题讨论】:

    标签: spring velocity code-injection map


    【解决方案1】:

    不要在这个用例中使用&lt;util:map&gt;。这实际上是MapFactoryBean 的别名。 改用经典的&lt;map&gt; 元素,它应该可以工作。

    【讨论】:

    • 我将 与 一起使用,删除了 util 命名空间并保持其他所有内容相同,但是我得到一个异常:元素类型“ref:bean”必须后跟任一属性规范, ">" 或 "/>"
    • 地图请使用&lt;entry key="number" value-ref="date-bean"/&gt;
    • 我也尝试过:我得到以下异常:映射 [java.util.Map] 的键类型 [class java.lang.Object] 必须可分配给 [java.lang.String]
    • 你能告诉我你的服务类中 toolMap 属性的声明吗?
    • 和 有什么区别?我是否必须配置 MapFactoryBean 才能使用 ?
    【解决方案2】:

    您的 XML 标头应如下所示(注意正确的标头声明):

    <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"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    
    <!-- ... -->
    

    如C.2.2 The util schema 中所述。

    【讨论】:

    • xml 确实包含您提到的声明。
    • @aces。 xsi:schemaLocation 也在吗?
    • 是的...为了简洁起见,我决定不添加文件的其余部分。你能看看 costi 的 cmets,我一直在尝试各种排列和组合来修复我遇到的错误。
    猜你喜欢
    • 2019-05-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多