Spring Tutorial - Spring Map Properties








We can fill value or a list of values to a Java bean defined in Spring xml configuration file.

The following sections shows how to fill data to Map.

Java Bean

In order to show how to use xml configuration file to fill collection properties, we defined a Customer object with four collection properties.

package com.java2s.common;

import java.util.HashMap;
import java.util.Map;

public class Customer {
  private Map<Object, Object> maps = new HashMap<Object, Object>();

  public String toString() {
    return maps.toString();
  }

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

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

Here is the Person bean.

package com.java2s.common;

public class Person {
  private String name;
  private int age;
  private String address;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getAddress() {
    return address;
  }

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

  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + ", address=" + address
        + "]";
  }  
}




Map

The following code shows how to fill data to a java.util.Set typed property.

The code fills in three values. The first one is hard-coded value 1. The second one is a bean reference. We have to define PersonBean somewhere in order to use it here. The third one is a bean definition with property-setting. When filling data for java.util.Map we have to provide key and value pair.

...
<property name="maps">
    <map>
      <entry key="Key 1" value="1" />
      <entry key="Key 2" value-ref="PersonBean" />
      <entry key="Key 3">
        <bean class="com.java2s.common.Person">
          <property name="name" value="java2sMap" />
          <property name="address" value="address" />
          <property name="age" value="28" />
        </bean>
      </entry>
    </map>
</property>
...




Example

Full Spring's bean configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.java2s.common.Customer">
    <!-- java.util.Map -->
    <property name="maps">
      <map>
        <entry key="Key 1" value="1" />
        <entry key="Key 2" value-ref="PersonBean" />
        <entry key="Key 3">
          <bean class="com.java2s.common.Person">
            <property name="name" value="java2sMap" />
            <property name="address" value="address" />
            <property name="age" value="28" />
          </bean>
        </entry>
      </map>
    </property>
  </bean>
  <bean id="PersonBean" class="com.java2s.common.Person">
    <property name="name" value="java2s1" />
    <property name="address" value="address 1" />
    <property name="age" value="28" />
  </bean>
</beans>

Here is the code to load and run the configuration.

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

Output



Download Java2s_Spring_Map_Properties.zip

MapFactoryBean

MapFactoryBean class can create a Map collection class HashMap or TreeMap in Spring's bean configuration file.

The following code shows how to create a HashMap, fill data and then inject it into a bean property.

Here is the Java bean class.

package com.java2s.common;
//from w  w  w .  ja va2 s  .  c  om
import java.util.HashMap;
import java.util.Map;

public class Customer {
  private Map<Object, Object> maps = new HashMap<Object, Object>();

  public String toString() {
    return maps.toString();
  }

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

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

Here is Spring's bean configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="CustomerBean" class="com.java2s.common.Customer">
    <property name="maps">
      <bean class="org.springframework.beans.factory.config.MapFactoryBean">
        <property name="targetMapClass">
          <value>java.util.HashMap</value>
        </property>
        <property name="sourceMap">
          <map>
            <entry key="Key1" value="1" />
            <entry key="Key2" value="2" />
            <entry key="Key3" value="3" />
          </map>
        </property>
      </bean>
    </property>
  </bean>
</beans>


Download Java2s_Spring_MapFactoryBean.zip

util schema

We also can use util schema and <util:map> to fill data to java.util.Map.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-2.5.xsd">
  <bean id="CustomerBean" class="com.java2s.common.Customer">
    <property name="maps">
      <util:map map-class="java.util.HashMap">
        <entry key="Key1" value="1" />
        <entry key="Key2" value="2" />
        <entry key="Key3" value="3" />
      </util:map>
    </property>
  </bean>
</beans>


Download Java2s_Spring_Map_util.zip

Here is the code to run the application.

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("CustomerBean");
      System.out.println(cust);
    }
}

The code above generates the following result.