Main Tutorials

Spring MapFactoryBean example

The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.

Here’s a MapFactoryBean example, it will instantiate a HashMap at runtime,, and inject it into a bean property.


package com.mkyong.common;

import java.util.Map;

public class Customer 
{
	private Map maps;
	//...
}

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.mkyong.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>

Alternatively, you also can use util schema and <util:map> to achieve the same thing.


<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.mkyong.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>

Remember to include the util schema, else you will hit the following error

 
Caused by: org.xml.sax.SAXParseException: 
	The prefix "util" for element "util:map" is not bound.

Run it…


package com.mkyong.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);
    	
    }
}

Ouput


Customer [maps={Key2=2, Key1=1, Key3=3}] Type=[class java.util.HashMap]

You have instantiated a HashMap and injected it into Customer’s map property at runtime.

Download Source Code

Download It – Spring-MapFactoryBean-Example.zip (5KB)

Reference

  1. MapFactoryBean Javadoc

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
suraj
12 years ago

Hi MKyoung,can u please tell me how to inject map,set,properties value using annotations.My intension is not to see cfg file at all.
If possible please reply to my mail with a small example

mostypig
5 years ago

Thank you for the tutorial.
I have only one question, why do you want to write XML instead of just having java code. It looks fancy in a small example, in production code it’s a f****ing hell. Especially, if you prod code is written by engineers from India who are paid 30 cents per hour.

rfr
10 years ago

What is the generating the output: Type=[class java.util.HashMap] ? The toString in Customer only returns: return “Customer [maps=” + maps + “]”;

satish
11 years ago

im my system the cust object display the “memory” not data. What’s wrong ?? Every application like shows the object memory

Pls Reply

Thanks
satish