Share on Google+Share on Google+

Spring Map Property

In this tutorial you will learn about the spring map property.

Spring Map Property

The Spring Framework has bean support for the Collections. It provide list, set, map and props elements. Here in this tutorial you will see about the map elements which is used to set values inside the map.


import java.util.Map;

public class CollegeBean {
  private Map<Object, Object> maps;

  public Map<Object, Object> getMaps() {
    return maps;

  public void setMaps(Map<Object, Object> maps) {
    this.maps = maps;

  public String toString() {
    return "College [Maps=" + maps + "]";

package collection.set.example;

public class StudentBean {
  private String name;
  private String address;

  public String getName() {
    return name;

  public void setName(String name) { = name;

  public String getAddress() {
    return address;

  public void setAddress(String address) {
    this.address = address;

  public String toString() {
    return "Student [address=" + address + ", name=" + name + "]";


import org.springframework.beans.factory.BeanFactory;

public class MapMain {
   public static void mainString[] args )
    BeanFactory beanfactory = new ClassPathXmlApplicationContext("context.xml");
      CollegeBean coll = (CollegeBean)beanfactory.getBean("collegeBean2");


<!-- Spring Map Property -->
    <bean id="collegeBean2" class="">
    <property name="maps">
      <entry key="Key 1" value="1" />
      <entry key="Key 2" value-ref="studentBean2" />
      <entry key="Key 3">
        <bean class="">
          <property name="name" value="mkyongMap" />
          <property name="address" value="address" />


  <bean id="studentBean2" class="">
    <property name="name" value="mkyong1" />
    <property name="address" value="address 1" />
<!-- End -->

When you run this application it will display output as shown below:

College [Maps={Key 1=1, Key 2=Student [address=pune, name=suraj], Key 3=Student [address=patna, name=satya]}]

Download this example code


Posted on: August 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.