This is 7 of 13 parts of tutorial series
For configuring spring in your eclipse ide please refer hello world example
here for initializing collectios(list) i.e. listofStates attribute of country class, we have used <list> tag.
Tutorial Content:
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.
Part-1:Introduction to spring framework
Part-2:Dependency injection(ioc) in spring
Part-3:Spring hello world example in eclipse
Part-4:Dependency injection via setter method in spring
Part-5:Dependency injection via constructor in spring
Part-6:Spring Bean scopes with examples
Part-7:Initializing collections in spring
Part-8:Beans Autowiring in spring
Part-9:Inheritance in Spring
Part-10:Spring ApplicationContext
Part-11:Spring lifetime callbacks
Part-12:BeanPostProcessors in Spring
Part-13:Annotation based Configuration 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.
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)
{
System.out.println(state);
}
}
}
2.InitializingCollectionsMain.java
This class contains main function.Create InitializingCollectionsMain.java under package org.arpit.javapostsforlearning.Copy following content into InitializingCollectionsMain.java
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.
3.ApplicationContext.xml
<?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>
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:
Maharastra
Madhya Pradesh
Rajasthan
Source code:
Source:Download without jars files
Source + lib: Download with jars files