How to specify in Spring XML configuration file class for a Map property?

You can use util:map

<util:map id="someId" map-class="java.util.HashMap">
    <entry key="key1">
        <value>value1</value>
    </entry>
</util:map>

<bean id="a" class="A">
    <property name="mapProperty" ref="someId">
    </property>
</bean>

Don't forget to add the util namespace.


You can use util:map tag from the util schema. Here's an example:

<util:map id="utilmap" map-class="java.util.HashMap">
    <entry key="key1" value="value1"/>
    <entry key="key2" value="value2"/>
</util:map>

<bean id = 'a' class="A">
   <property name="mapProperty" ref="utilmap" />
</bean>

BTW, you should not use raw type HashMap. Use a parameterized type instead - HashMap<String, String>.

Tags:

Java

Spring