In this example I am going to give example for array list configuration in spring application using XML bean configuration file. This is my bean, here I have created the setter and getter method for array list variable ("countryList").
import java.util.ArrayList;
public class ArrayListConfig
{
import java.util.ArrayList;
private
ArrayList<String> countryList;
public
ArrayList<String> getCountryList() {
return countryList;
}
public void
setCountryList(ArrayList<String> countryList) {
this.countryList = countryList;
}
}
This is my beans.xml file. The <list> tag is used for configuring list.
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- Array
list configuration -->
<bean id="countryListBean"
class="com.listconfig.ArrayListConfig">
<property name="countryList">
<list>
<value>India</value>
<value>US</value>
<value>UK</value>
<value>China</value>
</list>
</property>
</bean>
</beans>
This is my main method for testing.
import
java.util.ArrayList;
import
java.util.ListIterator;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.FileSystemXmlApplicationContext;
public class ArrayListDemo {
public static void main(String[]
args) {
ApplicationContext
context = new FileSystemXmlApplicationContext("classpath:beans.xml");
ArrayListConfig
alistConfig = context.getBean("countryListBean",ArrayListConfig.class);
ArrayList<String>
countryList = alistConfig.getCountryList();
ListIterator<String>
lIterator = countryList.listIterator();
System.out.println("******************");
while(lIterator.hasNext()){
System.out.println(lIterator.next());
}
}
}
You will get result like this.
Comments