Share on Google+Share on Google+

Spring Map Factory, Spring Map Configuration

In this tutorial you will learn about Spring Map Factory and also see how to use map in spring framework.

Spring Map Factory

Ths MapFactoryBean is a simple factory for shared Map instance. The map element is defined in the xml bean definitions. The setSourceMap method set the source Map which is written in xml map element. The SetTargetListClass method set the class to use target Map.

package list.factory.example;

import java.util.Map;

public class Week {
	private Map days;

	public Map getDays() {
		return days;

	public void setDays(Map days) {
		this.days = days;

	public String toString() {
		return "Days [lists=" + days + "]";


package list.factory.example;

import org.springframework.context.ApplicationContext;

public class AppMain {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "context.xml" });
		Week weekday = (Week) context.getBean("week");


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
  xmlns:xsi="" xmlns:aop=""

<!-- Map Factory  -->

  <bean id="week2" class="map.factory.example.Week">
    <property name="days2">
      <bean class="org.springframework.beans.factory.config.MapFactoryBean">
        <property name="targetMapClass">
        <property name="sourceMap">
        <entry key="Key1" value="Sunday" />
        <entry key="Key2" value="Monday" />
        <entry key="Key3" value="Tuesday" />
        <entry key="Key4" value="Wednesday" />
        <entry key="Key5" value="Thursday" />
        <entry key="Key6" value="Friday" />
        <entry key="Key7" value="Saturday" />
<!-- End -->


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

Days [lists={Key2=Monday, Key1=Sunday, Key4=Wednesday, Key3=Tuesday, Key6=Friday, Key5=Thursday, Key7=Saturday}]

Download this example code



Posted on: August 31, 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.


Advertisement null