Thursday, March 26, 2015

Spring Basic - Collection List

Let's look at how we do the bean definitions to the List, Collection object.
Here also we use basic spring maven project and introduce the Point object which we use as a attribute in the Triangle to set the three points.
We are giving the tree points as a list to Triangle.
basic ode structure and package is as below
http://cgenit.blogspot.com/2015/03/aop-basic-application-code-and.html

The new class Point , we need to define three instances , as Triangle requires three points.
Point class will be as follows

Point .java
-------------------------------------------------------------------------------------------
package org.aop.model;

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;
}

@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}


}

-------------------------------------------------------------------------------------------

Triangle.java
-------------------------------------------------------------------------------------------
package org.aop.model;

import java.util.List;

public class Triangle {
private String name;
private List<Point> points;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Point> getPoints() {
return points;
}

public void setPoints(List<Point> points) {
this.points = points;
}

@Override
public String toString() {
return "Triangle [name=" + name + ", points=" + points + "]";
}


}

-------------------------------------------------------------------------------------------

spring.xml

Here we define bean definition to Point.java and add new attribute points type of List (Collection) to hold the three points.
We have defined a bean definition for each point.
When defining the collection or List attribute , we have used <list> tag and inside <list> tag we have given <ref>  tag to insert the values for the  list.
-------------------------------------------------------------------------------------------
<bean name="triangle" class="org.aop.model.Triangle">
<property name="name" value="Traingle name"></property>
<property name="points">
<list>
<ref bean="zeroPoint" />
<ref bean="pointTwo" />
<ref bean="pointThree" />
</list>
</property>
</bean>

<bean name="zeroPoint" class="org.aop.model.Point">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
<bean name="pointTwo" class="org.aop.model.Point">
<property name="x" value="20"></property>
<property name="y" value="0"></property>
</bean>
<bean name="pointThree" class="org.aop.model.Point">
<property name="x" value="0"></property>
<property name="y" value="20"></property>
</bean>

-------------------------------------------------------------------------------------------

Output
======================================================
Triangle [name=Traingle name, points=[Point [x=0, y=0], Point [x=20, y=0], Point [x=0, y=20]]]
======================================================

No comments:

Post a Comment