Initializing list of values using Spring
There can be a situation where we need to initialize a list of objects while loading the class.
In this article we will see how spring supports the collection objects. Let’s take an example of Line object . Here we have one start point location ,one end point location, and each of these has X and Y values eg: start(X,Y) and end(X,Y).
Spring has List , Map and Set interfaces to support for injecting the dependency of list of objects
In this example we can see how to configure the dependency of list of objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.j2eereference.spring; import java.util.List; public class Line { private List points; public List getPoints() { return points; } public void setPoints(List points) { this.points = points; } public void displayLinePoints() { for(Point point:points){ System.out.println("point is ("+point.getValueX()+","+point.getValueY()+")"); } } } |
In the above example code we have declared the member variable points as list, which is nothing but a list of Point objects.. We have the Point class as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.j2eereference.spring; public class Point { private int valueX; private int valueY; public int getValueX() { return valueX; } public void setValueX(int valueX) { this.valueX = valueX; } public int getValueY() { return valueY; } public void setValueY(int valueY) { this.valueY = valueY; } } |
Now We will see how to configure initializing the list values in spring.xml
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 |
<?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.xsd"> <bean id="LineBean" class="com.j2eereference.spring.Line"> <property name="points"> <list> <ref bean="startPoint"/> <ref bean="endPoint"/> </list> </property> </bean> <bean id="startPoint" class="com.j2eereference.spring.Point"> <property name="valueX" value="10"/> <property name="valueY" value="20"/> </bean> <bean id="endPoint" class="com.j2eereference.spring.Point"> <property name="valueX" value="20"/> <property name="valueY" value="30"/> </bean> </beans> |
Let’s run the below code to print the value of list objects which are configured in the spring.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.j2eereference.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String[] args) { ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml"); Line line= (Line)context.getBean("LineBean"); line.displayLinePoints(); } } |
output
12 May, 2015 12:50:08 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@16fd0b7: startup date [Tue May 12 00:50:08 IST 2015]; root of context hierarchy
12 May, 2015 12:50:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
point is (10,20)
point is (20,30)
Leave a Reply