Initializing collections in spring

This is 9 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

    Introduction to spring framework Spring interview questions Dependency injection(ioc) in spring Spring hello world example in eclipse Spring java based configuaration Dependency injection via setter method in spring Dependency injection via constructor in spring Spring Bean scopes with examples Initializing collections in spring Beans Autowiring in spring Inheritance in Spring Spring ApplicationContext Spring lifetime callbacks BeanPostProcessors in Spring Annotation based Configuration in spring Spring AOP tutorial
In previous posts we have seen how to initialize any string or reference via property's value tag or ref tag.In this post we will see how to initialize any collections in spring.
For configuring spring in your eclipse ide please refer  hello world example

This is simple pojo class having some attributes so here country has name and list of states.
Create under package org.arpit.javapostsforlearning.Copy following content into

package org.arpit.javapostsforlearning;

import java.util.List;

public class Country {

    String countryName;
    List<String> listOfStates;
    public String getCountryName() {
        return countryName;
    public void setCountryName(String countryName) {
        this.countryName = countryName;
    public List<String> getListOfStates() {
        return listOfStates;
    public void setListOfStates(List<String> listOfStates) {
        this.listOfStates = listOfStates;
    public void printListOfStates()
        System.out.println("Some of states in india are:");
        for(String state:listOfStates)

This class contains main function.Create under package org.arpit.javapostsforlearning.Copy following content into
package org.arpit.javapostsforlearning;

import org.springframework.context.ApplicationContext;

public class InitializingCollectionsMain{

    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Country countryObj = (Country) appContext.getBean("CountryBean");

You can note here that we have used ClassPathXmlApplicationContext for getting bean here.There are various ways for getting beans.In hello world example we have used XmlBeanFactory for getting beans.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:xsi="" xmlns:aop=""

 <bean id="CountryBean" class="org.arpit.javapostsforlearning.Country">
  <property name="listOfStates">
    <value>Madhya Pradesh</value>



here for initializing collectios(list) i.e. listofStates attribute of country class, we have used <list> tag.
In <list> tag ,you can have <value> tag or <ref> tag for adding values in list.

4.Run it

When you will run above application,you will get following as output.
Some of states in india are:
Madhya Pradesh

Source code:

In next post,we will see beans autowiring in spring.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012