Spring MapFactoryBean

It’s a simple factory for shared Map instances. It allows for central setup of Maps via the “map” element in XML bean definitions.

Following example demonstrates Spring Map Factory:

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml




Create Address Class as following.

package com.kruders.bean;

public class Address {
	private String city;
	private String country;
	public String getCity() {
		return city;
	public void setCity(String city) {
		this.city = city;
	public String getCountry() {
		return country;
	public void setCountry(String country) {
		this.country = country;

Now create Employee Class as following

package com.kruders.bean;

import java.util.Map;

public class Employee {
	private String name;
	private Map<String,Address> maps;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public Map<String, Address> getMaps() {
		return maps;
	public void setMaps(Map<String, Address> maps) {
		this.maps = maps;

2. Configuration

Create Spring-Bean.xml and write the following code

<?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.xsd">

	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	   <property name="maps">
            <bean class="org.springframework.beans.factory.config.MapFactoryBean">
                <property name="targetMapClass">
                <property name="sourceMap">
                    	<entry key="1">
	                    	<bean class="com.kruders.bean.Address">
								<property name="city" value="Delhi" />
								<property name="country" value="India" />

3. Run Program

Create Main.java class that displays the employee details and run it as Java Application

package com.kruders.core;

import java.util.Map;

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

import com.kruders.bean.Address;
import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		System.out.println("\nPrinting Map");
		for(Map.Entry<String, Address> entry : employee.getMaps().entrySet()) {
			System.out.print("City " + entry.getValue().getCity());
			System.out.println(", Country " + entry.getValue().getCountry());

When you run the above example you’ll get an output like:

Name Puneet

Printing Map
City Delhi, Country India

The folder structure of the example is shown below in Figure 41.1

Figure 41.1 Figure 41.1

You can download the source code of this example here.


No comments yet.

Leave a Reply