Supported collections are:
- <list>
- <set>
- <map>
---------------------------------------------------------------------
1. <list>
- DrawingApp class remains the same, however refering to the Triangle Collection bean this time as below.
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
TriangleCollection triangleCollection = (TriangleCollection) context.getBean("triangleCollection");
triangleCollection.Draw();
}
}
- New Triangle class, TriangleCollection modified for accepting List for Point objects as shown below
public class TriangleCollection {
private List<Point> pointsList;
public List<Point> getPointsList() {
return pointsList;
}
public void setPointsList(List<Point> pointsList) {
this.pointsList = pointsList;
}
public void Draw()
{
for(Point point: pointsList)
{
System.out.println("Point: (" + point.getX() + "," + point.getY()+")");
}
}
}
- The spring.xml would be modified as below. The<ref> tags are used within <list> tags with the bean attribute to initiate the Point objects form within the "triangleCollection" bean.
<beans>
<bean id="triangleCollection" class="com.product.springDemo.TriangleCollection">
<property name="pointsList">
<list>
<ref bean ="Bean-pointB"/>
<ref bean ="Bean-pointC"/>
</list>
</property>
</bean>
<bean id="Bean-pointB" class="com.product.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>
<bean id="Bean-pointC" class="com.product.springDemo.Point">
<property name="x" value="30"/>
<property name="y" value="300"/>
</bean>
</beans>
2. <set>
- DrawingApp class remains the same as above
- New Triangle class, TriangleCollection modified for accepting Set for Point objects as shown below
public class TriangleCollection {
private Set<Point> pointsList;
public Set<Point> getPointsList() {
return pointsList;
}
public void setPointsList(Set<Point> pointsList) {
this.pointsList = pointsList;
}
public void Draw()
{
for(Point point: pointsList)
{
System.out.println("Point: (" + point.getX() + "," + point.getY()+")");
}
}
}
- The spring.xml would be modified as below. The<ref> tags are used within <set> tags with the bean attribute to initiate the Point objects form within the "triangleCollection" bean.
<beans>
<bean id="triangleCollection" class="com.product.springDemo.TriangleCollection">
<property name="pointsList">
<set>
<ref bean ="Bean-pointB"/>
<ref bean ="Bean-pointC"/>
</set>
</property>
</bean>
<bean id="Bean-pointB" class="com.product.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>
<bean id="Bean-pointC" class="com.product.springDemo.Point">
<property name="x" value="30"/>
<property name="y" value="300"/>
</bean>
</beans>
No comments:
Post a Comment