Initializing collections in spring

This is 9 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

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

1.Country.java:

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

2.InitializingCollectionsMain.java

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

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.

3.ApplicationContext.xml


here for initializing collectios(list) i.e. listofStates attribute of country class, we have used tag.

In tag ,you can have tag or tag for adding values in list.

4.Run it

When you will run above application,you will get following as output.

Source code:

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

Add Comment