Spring Collections (List, Set, Map, and Properties) example

Spring examples to show you how to inject values into collections type (List, Set, Map, and Properties). 4 major collection types are supported :

  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

Spring beans

A Customer object, with four collection properties.


package com.mkyong.common;

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

public class Customer 
{
	private List<Object> lists;
	private Set<Object> sets;
	private Map<Object, Object> maps;
	private Properties pros;
	
	//...
}

See different code snippets to declare collection in bean configuration file.

1. List example


	<property name="lists">
		<list>
			<value>1</value>
			<ref bean="PersonBean" />
			<bean class="com.mkyong.common.Person">
				<property name="name" value="mkyongList" />
				<property name="address" value="address" />
				<property name="age" value="28" />
			</bean>
		</list>
	</property>

2. Set example


	<property name="sets">
		<set>
			<value>1</value>
			<ref bean="PersonBean" />
			<bean class="com.mkyong.common.Person">
				<property name="name" value="mkyongSet" />
				<property name="address" value="address" />
				<property name="age" value="28" />
			</bean>
		</set>
	</property>

3. Map example


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

4. Properties example


	<property name="pros">
		<props>
			<prop key="admin">admin@nospam.com</prop>
			<prop key="support">support@nospam.com</prop>
		</props>
	</property>

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.mkyong.common.Customer">

		<!-- java.util.List -->
		<property name="lists">
			<list>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.mkyong.common.Person">
					<property name="name" value="mkyongList" />
					<property name="address" value="address" />
					<property name="age" value="28" />
				</bean>
			</list>
		</property>

		<!-- java.util.Set -->
		<property name="sets">
			<set>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.mkyong.common.Person">
					<property name="name" value="mkyongSet" />
					<property name="address" value="address" />
					<property name="age" value="28" />
				</bean>
			</set>
		</property>

		<!-- 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.mkyong.common.Person">
						<property name="name" value="mkyongMap" />
						<property name="address" value="address" />
						<property name="age" value="28" />
					</bean>
				</entry>
			</map>
		</property>

		<!-- java.util.Properties -->
		<property name="pros">
			<props>
				<prop key="admin">admin@nospam.com</prop>
				<prop key="support">support@nospam.com</prop>
			</props>
		</property>

	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>

</beans>

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);
    	
    }
}

Output


Customer [

lists=[
1, 
Person [address=address 1, age=28, name=mkyong1], 
Person [address=address, age=28, name=mkyongList]
], 

maps={
key 1=1,
key 2=Person [address=address 1, age=28, name=mkyong1], 
key 3=Person [address=address, age=28, name=mkyongMap]
}, 

pros={admin=admin@nospam.com, support=support@nospam.com}, 

sets=[
1, 
Person [address=address 1, age=28, name=mkyong1], 
Person [address=address, age=28, name=mkyongSet]]
]

Download Source Code

Download it – Spring-Collection-Example.zip (6 KB)

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
Kami
Guest
Kami

Hi, MKyong which implementation uses spring in Collection???

arun
Guest
arun

Very good explanation

Vivek
Guest
Vivek

Is it not possible that ” Person [address=address 1, age=28, name=mkyong1], ” does not get repeated with every piece of data ?

Vivek
Guest
Vivek

Is it not possible that ” Person [address=address 1, age=28, name=mkyong1], ” does not get repeated with every piece of data ?

Younis Irshad
Guest
Younis Irshad

NEED TO ADD in CLASS PERSON:

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

baris
Guest
baris

Put it in your Person class

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

Rahul Sachdeva
Guest
Rahul Sachdeva

Hi Team,
I am confused on this property name thing.I am new to Springs.
In Java a list is not in the key-value format but here Property name is also associated with value..i.e. may be the value at an index in the say arraylist.

My Requirement is :
Simply create an arraylist in springs ,using this XML technique..add some elements to it and iterate it.Please assist.

Rahul.

lamuria
Guest
lamuria

Some how it does not work when i define list as:

1

But works when I remove .. and :

Rahman
Guest
Rahman

i had the same problem and it worked if i removed the able mentioned tags with the reference on the bottom.

yujian
Guest
yujian

Thanks. I like to click ads.

trackback
Instantiating Collections in Application Context XML Files | Tidbits from the Web

[…] mkyong‘s summary of the collection elements (actually nicer than my entry here ) […]

koti
Guest
koti

Thanks mkyong…….

hiway
Guest
hiway

sometimes, arguments are stored in external files, ie, .properties file, so we can import ‘util’ namespace to inject .properties file to property.

xmlns:util=”http://www.springframework.org/schema/util”

trackback
Instantiating Collections in Application Context XML Files | Sven Logan's Blog

[…] mkyong‘s summary of the collection elements (actually nicer than my entry here ) […]

David
Guest
David

Thank you

Bhooma
Guest
Bhooma

Thanks Mkyong for explaining Spring in simple way.

List, Set and Map are interfaces. Can you explain the implemented classes used by Spring for respected interfaces.

Plap
Guest
Plap

Nice post!

but what if I have to share the same properties/map/list/set among more beans? I’ve to make it like a bean and use ref? How I could?

govind
Guest
govind

thanks to mkyong…
i want more explantion about this please send to my mail id

Tiny Tim
Guest
Tiny Tim

Hey! I use your page all the time!

You’d think I would eventually remember them, but they seem so random to me!

Thanks for putting this together!

Rachid
Guest
Rachid

Firstly, thank you very much MKYong

I tried your example but I got other output:

Customer [lists=[1, com.mkyong.common.Person@4839e5b5, com.mkyong.common.Person@7b5a6029], sets=[1, com.mkyong.common.Person@4839e5b5, com.mkyong.common.Person@5117f31e], maps={Key 1=1, Key 2=com.mkyong.common.Person@4839e5b5, Key 3=com.mkyong.common.Person@6a5f6303}, pros={admin=admin@nospam.com, support=support@nospam.com}]

Can any body help me to get a write output?

Srinivas
Guest
Srinivas

Override default toString() method of Person class with the following code

public String toString() {
	return "Person [address=" + address + ", age=" + age + ", name=" + name + "]";
}
Chaitralee Shelar
Guest
Chaitralee Shelar

Srinivas Thank you.

Pavan Karasala
Guest
Pavan Karasala

This is the right way to convert.

Yathi
Guest
Yathi

I guess you have to override toString() method of Person Class

Anand
Guest
Anand

Hi Yong
This is a great service you are injecting for developers. Thanks and Admire your example of simplicity.

Rachid
Guest
Rachid

I tried the example but I got other output:

Can you help me to get a right output?

trackback
Gik notes » List in Spring

[…] may be useful http://www.mkyong.com/spring/spring-collections-list-set-map-and-properties-example/ Posted on February 27, 2012 by gik. This entry was posted in Java and tagged java, spring. […]

Sujee
Guest
Sujee

Thank you MKYong. It’s really helped me to understand various ways of configuring collections.

trackback
Spring Tutorials | Tutorials

[…] Collections (List, Set, Map, and Properties) example How to define collections type (List, Set, Map, and Properties) in Spring’s bean configuration file. […]

trackback
Fly Fishing for Largemouth Bass | Bass Fishing Blogger

[…] Spring – Collections (List, Set, Map, &#1072&#1495&#1281 Properties) example | Spring […]

trackback
Basic motion sensor alarm introduction | Motion Sensor Alarm

[…] Spring – Collections (List, Set, Map, and Properties) example | Spring […]