Search code examples
javaspringdependency-injectionhashmapapplicationcontext

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

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.

    <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! =:)


Solution

  • I tinkered some more and got it to work by removing "value" elements that enclosed the util:list elements. 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!