Aravind Aravind - 3 months ago 10
Java Question

How to inject a Map<String, List> in java springs?

How to inject a Map in java spring framework?
If possible please provide some sample code.

Is the following legal?

<property name="testMap">
<map>
<entry>
<key>
<value>test</value>
</key>
<value>
<list>
<value>String</value>
<value>String</value>
</list>
</value>
</entry>
</map>
</property>

Answer

Define a Map like this first inside your applicationContext.xml:

   <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 id="myBean" class="com.sample.beans">
      <property name="emailMap" ref="emailMap" />
   </bean>
Comments