Monday, July 22, 2013

09-Initializing-Collections

DrawingApp.java
package org.yash.watertechsol;

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


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
package org.yash.watertechsol;

import java.util.List;

public class Triangle {
     
      private List<Point> points;

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

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

      public void draw(){
            for(Point point:points){
                  System.out.println("(" + point.getX()+", "+point.getY()+")");
            }
      }


}

Point.java
package org.yash.watertechsol;

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="org.yash.watertechsol.Triangle">
            <property name="points">
                  <list>
                        <ref bean="point1"></ref>
                        <ref bean="point2"></ref>
                        <ref bean="point3"></ref>
                  </list>
            </property>
      </bean>

     
      <bean id ="point1" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="0" />
      </bean>
     
      <bean id ="point2" class="org.yash.watertechsol.Point">
            <property name="x" value="-20" />
            <property name="y" value="0" />
      </bean>

      <bean id ="point3" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="20" />
      </bean>    
</beans>



No comments:

Post a Comment