Spring - Map configuration using xml bean configuration file

 This is very simple example for configuring Map in spring application using XML bean configuration file and this is my bean class.

package com.mapconfig;

import java.util.Map;

public class MapConfig {
      Map<String,String> demoMap ;

      public Map<String, String> getDemoMap() {
            return demoMap;

      public void setDemoMap(Map<String, String> demoMap) {
            this.demoMap = demoMap;

The corresponding beans.xml file is here.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
      <!-- Map configuration -->
      <bean id="mapConfigBean" class="com.mapconfig.MapConfig">
            <property name="demoMap">
                        <entry key="key1" value="value1"></entry>
                        <entry key="key2" value="value2"></entry>
                        <entry key="key3" value="value3"></entry>
                        <entry key="key4" value="value4"></entry>

The <map> tag is used for configuring map - key value pair. This is my main program for testing.

package com.mapconfig;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MapDemo {
      public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");
            MapConfig mapConfigBean = context.getBean("mapConfigBean",MapConfig.class);
            Map<String,String> demoMap = mapConfigBean.getDemoMap();
            Set<String> keys = demoMap.keySet();
            Iterator<String> iterator = keys.iterator();
            while(iterator.hasNext()) {
                  String key = (String) iterator.next();
                  System.out.println("The map key is " + key + " and the corresponding value is " + demoMap.get(key));