【问题标题】:How to construct Map<String, List<String>> data structure in SpringSpring中如何构造Map<String, List<String>>数据结构
【发布时间】:2014-02-11 05:15:46
【问题描述】:

我正在尝试在 Spring 中实现这个 Java 数据结构(我是新手):

Map<String, List<String>> 

我尝试了以下(及其变体),但出现以下异常:

Caused by: org.xml.sax.SAXParseException; lineNumber: XX; columnNumber: YY; cvc-complex-type.2.4.d: Invalid content was found starting with element 'util:list'. No child element is expected at this point.

谁能告诉我我犯的错误?我需要能够使用文字键(字符串)和值列表构建上述“映射”数据结构。我包含 twp 完整示例“条目”(不起作用)只是为了显示我正在寻求创建的填充模式。

    <bean .... >
      ...
    <property name="monitoredObjects">
        <util:map map-class="java.util.HashMap">
            <entry key="java.lang:type=GarbageCollector,name=ConcurrentMarkSweep">
                <value>
                    <util:list>
                        <value>HeapMemoryUsage</value>
                        <value>NonHeapMemoryUsage</value>
                    </util:list>
                </value>
            </entry>

            <entry key="java.lang:type=FOO,name=BAR">
                <value>
                    <util:list>
                        <value>YADA-YADA</value>
                        <value>BLAH-BLAH</value>
                    </util:list>
                </value>
            </entry>
        </util:map>
    </property>
      ...
    </bean>

谢谢! =:)

【问题讨论】:

    标签: java spring dependency-injection hashmap applicationcontext


    【解决方案1】:

    我进行了更多修改,并通过删除包含 util:list 元素的“值”元素来使其工作。换句话说,像这样:

    <bean .... >
        ...
       <property name="monitoredObjects">
           <util:map map-class="java.util.HashMap">
    
               <entry key="java.lang:type=GarbageCollector,name=ConcurrentMarkSweep">
                       <util:list>
                           <value>HeapMemoryUsage</value>
                           <value>NonHeapMemoryUsage</value>
                       </util:list>
               </entry>
    
               <entry key="java.lang:type=FOO,name=BAR">
                       <util:list>
                           <value>YADA-YADA</value>
                           <value>BLAH-BLAH</value>
                       </util:list>
               </entry>
    
           </util:map>
       </property>
       ...
    </bean>
    

    一如既往的感谢您的关注!

    【讨论】:

      【解决方案2】:

      首先在 applicationContext.xml 中定义这样的 Map:

       <util:list id="list1">
            <value>foo@bar.com</value>
            <value>foo1@bar.com</value>
         </util:list>
      
         <util:list id="list2">
            <value>foo2@bar.com</value>
            <value>foo3@bar.com</value>
         </util:list>
      
         <util:map id="emailMap" value-type="java.util.List">
            <!-- Map between String key and List -->
            <entry key="entry1" value-ref="list1" />
            <entry key="entry2" value-ref="list2" />
            ...
         </util:map>
      

      然后像这样在你的任何 bean 中使用这个 Map:

      <bean id="myBean" class="com.sample.beans">
            <property name="emailMap" ref="emailMap" />
         </bean> 
      

      【讨论】:

      • 嗨阿迪亚。您的示例可以工作(事实上,我在搜索时在其他地方看到了相同的解决方案),但我想要一个所描述的“在线”解决方案,而不是中断。不过谢谢。 :)
      猜你喜欢
      • 2020-07-14
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多