Table of Contents
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 XML based configuration example
- 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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.javapostsforlearning; import java.util.List; public class Country { String countryName; List listOfStates; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public List getListOfStates() { return listOfStates; } public void setListOfStates(List listOfStates) { this.listOfStates = listOfStates; } public void printListOfStates() { System.out.println("Some of states in india are:"); for(String state:listOfStates) { System.out.println(state); } } } |
2.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="CountryBean" class="org.arpit.javapostsforlearning.Country"> <property name="listOfStates"> <list> <value>Maharastra</value> <value>Madhya Pradesh</value> <value>Rajasthan</value> </list> </property> </bean> </beans> |
In tag ,you can have tag or tag for adding values in list.
3.InitializingCollectionsMain.java
This class contains main function.Create InitializingCollectionsMain.java under package org.arpit.javapostsforlearning.Copy following content into InitializingCollectionsMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.arpit.javapostsforlearning; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InitializingCollectionsMain{ public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("CountryBean"); countryObj.printListOfStates(); } } |
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.
4.Run it
When you will run above application,you will get following as output.
1 2 3 4 5 6 |
Some of states in india are: Maharastra Madhya Pradesh Rajasthan |
That’s all about Initializing collections in spring.
In next post,we will see beans autowiring in spring.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.