[英]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.