Pages Navigation Menu

Coding is much easier than you think

List FactoryBean, Set FactoryBean and Map FactoryBean in Spring

List FactoryBean, Set FactoryBean and Map FactoryBean in Spring



We have previously seen how to deal with List, Set and Map type attributes inside a bean class. Further let us venture into having concrete List collection (ArrayList and LinkedList), Set collection(HashSet and TreeSet)  Map collection(HashMap and TreeMap)


Step 1 : Create the POJO

File : ClassABC.java

package com.simpleCodeStuffs;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class ClassABC {

	private List listVariable;
	private Map mapVariable;
	private Set setVariable;

	public List getListVariable() {
		return listVariable;

	public void setListVariable(List listVariable) {
		this.listVariable = listVariable;

	public Map getMapVariable() {
		return mapVariable;

	public void setMapVariable(Map mapVariable) {
		this.mapVariable = mapVariable;

	public Set getSetVariable() {
		return setVariable;

	public void setSetVariable(Set setVariable) {
		this.setVariable = setVariable;

	public String toString() {
		return ("listVariable t" + listVariable + "nsetVariable t"
				+ setVariable + "nmapVariable t" + mapVariable);

File : Amount.java

package com.simpleCodeStuffs;

public class Amount {

	private String val;

	public String getVal() {
		return val;

	public void setVal(String val) {
		this.val = val;

	public String toString() {
		return ("amount bean val: " + val);


Step 2 : Provide the configuration metadata in the xml file


To support the concrete classes of List, Map and Set, it is essential to include util schema in the Beans.xml file.Else, you will end up getting a

SAXParseException because of it


File : Beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

<bean id="abc" class="com.simpleCodeStuffs.ClassABC">
	<property name="listVariable">
		<util:list list-class="java.util.ArrayList">
			<ref bean="amount" />

	<property name="setVariable">
		<util:set set-class="java.util.HashSet">
			<ref bean="amount" />

	<property name="mapVariable">
		<util:map map-class="java.util.HashMap">
			<entry key="Key1" value="3" />
			<entry key="Key2" value="HashMap" />
			<entry key="Key3" value-ref="amount" />

<bean id="amount" class="com.simpleCodeStuffs.Amount">
	<property name="val" value="simpleCodeStuffs" />


Step 3 :

File : MainClass.java

package com.simpleCodeStuffs;

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

public class MainClass 
public static void main(String[] args) 
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
	ClassABC abc = (ClassABC) context.getBean("abc");


Step 4: Run the application. The output is





About Gokul

I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

%d bloggers like this: