Let's
take an example to understand how to Initialize list in spring.
DrawingApp.java
public
class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle)
context.getBean("triangle");
triangle.draw();
}
}
Triangle.java
public
class Triangle {
private List<Point> points;
public List<Point> getPoints() {
return points;
}
public void setPoints(List<Point> points) {
this.points = points;
}
public void draw(){
System.out.println("Points are ");
for(Point point : points){
System.out.println("("+point.getX()+","+point.getY()+")"
);
}
}
}
Point.java
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Point.java
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
spring.xml
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="com.example4.Triangle">
<property name="points">
<list>
<ref
bean="zeroPoint" />
<ref
bean="point2" />
<ref
bean="point3" />
</list>
</property>
</bean>
<bean id="zeroPoint" class="com.example4.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>
<bean id="point2" class="com.example4.Point">
<property name="x" value="-20"/>
<property name="y" value="0"/>
</bean>
<bean id="point3" class="com.example4.Point">
<property name="x" value="20"/>
<property name="y" value="0"/>
</bean>
</beans>
Output : Points are
(0,0)
(-20,0)
(20,0)
If
you want to Initialize MAP then
you have to write like this in spring.xml
<property name="points">
<map>
<entry key="Key 1"
value="zeroPoint" />
<entry key="Key 2"
value-ref="point2" />
<entry key="Key 3">
<bean
class="com.example4.Point">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
</entry>
</map>
</property>
If
you want to Initialize SET then
you have to write like this in spring.xml
<property
name= "points">
<set>
<value>1</value>
<ref bean="zeroPoint" />
<bean class="com.example4.Point">
<property name="x" value="0" />
<property name="y" value="10" />
</bean>
<ref bean="point3" />
</set>
</
property>
If
you want to Initialize Properties
then you have to write like this in spring.xml
<property
name="pros">
<props>
<prop
key="admin">admin@motionwelder.com</prop>
<prop
key="support">support@motionwelder.com</prop>
</props>
</property>
Nice Article, covered every aspect of it.
ReplyDeleteWhat if there is n number of point? how to initialize n number of points?
ReplyDeletewhat if it is list of integers. List rollno. How to initialise it?
ReplyDeleteFirst example is copied from Kaushik ( Javabrains), including the point values. You should at least give credits to people you have copied from
ReplyDelete