Thursday, April 19, 2012

Spring Collection Example

package com.venkat.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DrawingApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");
        Triangle t=(Triangle)factory.getBean("triangleBean");
        t.draw();

    }

}
-------------------------------------------------------------------------
package com.venkat.core;

public class Point {

private int x;
private int y;
public int getY() {
    return y;
}
public void setY(int y) {
    this.y = y;
}
public int getX() {
    return x;
}
public void setX(int x) {
    this.x = x;
}

   
}
-------------------------------------------------------
package com.venkat.core;

import java.util.List;


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()
    {
        for (Point point : points)
        {
        System.out.println("Point  =("+point.getX()+","+point.getY() +")");
        }
    }
    }
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
        <bean id="triangleBean" class="com.venkat.core.Triangle">
        <property name="points" >
        <list>
        <ref bean="zeroPoint"/>
        <ref bean="point1"/>
        <ref bean="point2"/>
       
        </list>
        </property>
       
        </bean>
<bean id="zeroPoint" class="com.venkat.core.Point">
    <property name="x" value="0"/>
    <property name="y" value="0"/>
    </bean>
    <bean id="point1" class="com.venkat.core.Point">
    <property name="x" value="-20"/>
    <property name="y" value="0"/>
    </bean>
    <bean id="point2" class="com.venkat.core.Point">
    <property name="x" value="20"/>
    <property name="y" value="0"/>
    </bean>
</beans>

No comments:

Post a Comment