简体   繁体   English

如何构造地图 <String, List<String> &gt; Spring中的数据结构

[英]How to construct Map<String, List<String>> data structure in Spring

I'm trying to implement this Java data structure in Spring (which I am new to): 我正在尝试在Spring中实现这个Java数据结构(这是我的新手):

Map<String, List<String>> 

I tried the below (and variants of it), but am getting the following exception: 我尝试了以下内容(及其变体),但遇到以下异常:

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.

Can someone tell me the mistake I am making? 有人可以告诉我我犯的错误吗? I need to be able to build out the above mentioned "Map" data structure with literal Keys (String) and List of values. 我需要能够使用文字键(字符串)和值列表来构建上述“地图”数据结构。 I included twp complete sample "entries" (which are not working) just to show the fill-in pattern that I'm seeking to create. 我包括了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>

Thank you! 谢谢! =:) = :)

I tinkered some more and got it to work by removing "value" elements that enclosed the util:list elements. 我进行了一些修改,并通过删除包含在util:list元素中的“值”元素来使其工作。 In other words, like this: 换句话说,像这样:

<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>

Thanks as always for looking! 与往常一样感谢您的光临!

Define a Map like this first inside your applicationContext.xml: 首先在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>

Then use this Map in any bean of yours like this: 然后像这样在您的任何bean中使用此Map:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Map构造复杂的json结构 <String, String> 在java中 - How to construct a complex json structure using Map<String, String> in java 从字符串路径列表构造树结构 - Construct a tree structure from list of string paths 我如何将其写为列表 <map<string, string> java中的结构 - how do i write this as a list<map<string, string> structure in java 应用 Yml 地图<String,Map<String,List<Object> &gt;&gt; 弹簧靴结构 - Application Yml Map<String,Map<String,List<Object>>> in spring boot structure 如何将Firebase数据库中的数据构造为列表 <String> ? - How to structure data in firebase database as a List<String>? 我如何获取地图中存在的所有数据 <String,Map<String,String> &gt;数据结构? - How can i fetch all data present in Map<String,Map<String,String>> data structure? 如何更新列表中的数据 <? extends Map<String,?> &gt; - How to update data in a List<? extends Map<String,?>> 如何在地图上使用getProperty() <String, Map<String,String> &gt;结构 - How to use getProperty() on Map<String, Map<String,String>> structure 在spring中插入数据字符串List Map类型时如何修复null - How to fix null when insert data string List Map type in spring 如何以地图形式获取数据<String, List<String> &gt; 使用单个查询? - How to get data as a Map<String, List<String>> using a single query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM