Setting List Collection Values in Spring IoC

We have seen the process of setting class instances and primitive type values using Spring IoC, but can we provide a particular list of values to a bean in the form of List instance? Yes, we can. We can provide a list of values to a bean and the values in the list will be defined in Spring IoC container configuration file.

Tools used:

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

Our bean

We have modified our Person bean and added a new field of type List named “phone” that will store all phone numbers regarding to that person.

package com.raistudies.domain;

import java.util.ArrayList;
import java.util.List;

public class Person {
    private String name;
    private String address;
    private List phone = new ArrayList();

    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 List getPhone() {
        return phone;
    public void setPhone(List phone) { = phone;
    public String toString() {
        return "Name :" + + " and Address : " + this.address + "\nPhone Numbers : " + phone;

Here, we have assigned a ArrayList instance to phone property but actually, we will not need to do so because we will set the values from Spring IoC configuration file.

Spring IoC Container configuration file

You will see some new things in our Spring IoC 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">


Here, the property “phone” is set with a list values. <list> tag is used to set instance of ArrayList to the property with the values defines in between <list> tag with the help of <value> tag. You can define as many values as you want. Spring IoC will create an instance of ArrayList with these defined values and set to the property specified.

Run the example to test the output

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



Our test runner class will create the instance of BeanFactory using ClassPathXmlApplicationContext and load the configuration file, after that it will get the Person bean instance from BeanFactory and print the Person bean on console.

Run the example in eclipse indigo and you will get following output screen:

Spring IoC Setting List Values Output

Spring IoC Setting List Values Output

You can try the example yourself, just download the code from bellow links:

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>