Setting Map Collection Values in Spring IoC

Previous tutorial illustrates us how to set List Collection value using Spring IoC configuration file. In this tutorial, we will learn how to set Map Collection values using Spring IoC Container Configuration file. Our bean will contain a reference of type Map interface and we will set the Map entry values in Configuration file.

Tools used:

  • Spring IoC Framework 3.0.3
  • JDK 6
  • Eclipse Indigo 3.7

Our bean to hold Map instnace

package com.raistudies.domain;

import java.util.Map;

public class Person {
    private String name;
    private String address;
    private Map phone;

    public String getAddress() {
        return address;
    public void setAddress(String address) {
        this.address = address;
    public String getName() {
        return name;
    public void setName(String name) { = name;

    public Map getPhone() {
        return phone;
    public void setPhone(Map phone) { = phone;
    public String toString() {
        return "Name :" + + " and Address : "
        + this.address + "\nPhone Numbers : " + phone;

We have added a new property to our Person bean to hold cell numbers concerned to the person. The property name “phone” is of type Map and we will put the values in Spring IoC configuration file.

Spring IoC Container configuration file

Bellow is the Spring IoC configuration file to set the values to the properties of Person bean:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="person" class="com.raistudies.domain.Person">
        <property name="name" value="Rahul Mondal"/>
        <property name="address" value="Jaipur, India"/>
        <property name="phone">
                <entry key="Personal" value="+919912345678"/>
                <entry key="Office" value="+911411234567"/>

Here, you will get some new tags has been used to set the values to our bean property “phone”:

  • <map>: This tag is used to set the Map value to the property within which the tag has been define. The property must of type Map or implementation class of Map interface.
  • <entry>: The entry tag is used to define a single entry in the map with key – value pair specified in attribute “Key” and “value” respectively.

Run the example to test the output

package com.raistudies.runner;

import org.springframework.beans.factory.BeanFactory;

import com.raistudies.domain.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        BeanFactory factory = new ClassPathXmlApplicationContext("app-context.xml");
        Person person = (Person)factory.getBean("person");
        System.out.println("Person detail is : " + person);


To test the example, we will run our SpringBeanTestRunner class which will load the Spring IoC configuration file named “app-context.xml” and create instance of BeanFactory, get the Person bean from Spring IoC bean factory and print it’s values to console. We will get the following output:

Spring IoC Setting Map Values Output

Spring IoC Setting Map Values Output

Tutorial example is working well. You can also try the example by downloading source + lib from following links:

Source: Download

Source + lib: Download

Import the example code in Eclipse and run the example.

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>