Setting Set Collection Values in Spring IoC

We have learn how to set collection of type Map and List in Spring IoC. Now, in this example we will see to put Set collection values to Spring IoC bean property using Spring IoC configuration file. We will use same bean to implement our example but with only different, the bean will contain a property of type Set interface.

Tools used:

  • Spring IoC Framework 3.0.3
  • JDK 6
  • Eclipse Indigo 3.7

Bean class that contain Set property

We have used our Person bean to demonstrate our example.

package com.raistudies.domain;

import java.util.Set;

public class Person {
    private String name;
    private String address;
    private Set phone;

    public Set getPhone() {
        return phone;
    public void setPhone(Set phone) { = phone;

    public String getAddress() {
        return address;
    public void setAddress(String address) {
        this.address = address;
    public String getName() {
        return name;
    public void setName(String name) { = name;

    public String toString() {
        return "Name :" + + " and Address : "
        + this.address +"\nPhone: " + phone;

The bean contain a property of type Set “phone”, which will contain phone numbers of the person. The phone numbers will be set in Spring IoC configuration file along with name and address.

Spring IoC Container configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="person" class="com.raistudies.domain.Person">
        <property name="name" value="Rahul Mondal"/>
        <property name="address" value="Jaipur, India"/>
        <property name="phone">

You will find out some new tags in the above Spring IoC configuration file that are used to set the values of Set property “phone”. Here is the description of these tags:

  • <set>: is used to specify a Set instance to the property within which it has been defined.
  • <value>: is used to define unique values in Set instance.

Run the example to test the output

Following is the code to test our example:

package com.raistudies.runner;

import org.springframework.beans.factory.BeanFactory;

import com.raistudies.domain.Person;

public class SpringBeanTestRunner {

    public static void main(String[] args) {
        BeanFactory factory = new ClassPathXmlApplicationContext("app-context.xml");
        Person person = (Person)factory.getBean("person");
        System.out.println("Person detail is : " + person);



SpringBeanTestRunner code only loads the Spring IoC configuration file using ClassPathXmlApplicationContext and create instance of BeanFactory. Then we get the instance of Person bean from BeanFactory instance and print the values with the instance.

While running the example in Eclipse 3.7, we get the following output:

Spring IoC Setting Set Value Output

Spring IoC Setting Set Value Output

You can download source code of the example from bellow links and try yourself:

Source: Download

Source + lib: Download

Related Posts:

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>