In spring you can inherit the bean definition using keyword
“parent”
      <bean id="parentTriangle"
class="org.yash.watertechsol.Triangle">
            <property name="pointA"
ref="pointA"></property>
      </bean>     
      <bean id="triangle"
class="org.yash.watertechsol.Triangle" parent="parentTriangle">
            <property name="pointB"
ref="pointB"></property>
            <property name="pointC"
ref="pointC"></property>
      </bean>
If you don’t want to initialize your parent class just want
to use as template, then you have property class “abstract=true”, in this case
spring do not create beam for that id it can be only used as template. Child
beans have to inherit this bean.
      <bean id="parentTriangle"
class="org.yash.watertechsol.Triangle" abstract="true">
            <property name="pointA"
ref="pointA"></property>
      </bean>     
You can also add list or collection classes in bean
configuration.
      <bean id="parentTriangle"
class="org.yash.watertechsol.Triangle" abstract="true">
            <property name="points">
                  <list>
                        <ref bean="pointA" />
                  </list>
            </property>
      </bean>     
You can merge parent list in child bean list. If you
won’t merge then parent list over written by child bean.
      <bean id="parentTriangle"
class="org.yash.watertechsol.Triangle" abstract="true">
            <property name="points">
                  <list>
                        <ref bean="pointA" />
                  </list>
            </property>
      </bean>
      <bean id="triangle"
class="org.yash.watertechsol.Triangle" parent="parentTriangle">
            <property name="points">
                  <list merge="true">
                        <ref bean="pointB"
/>
                  </list>
            </property>       
      </bean>
Drawing.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();
                }
}
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;
      }
}
Triangle.java
package org.yash.watertechsol;
import java.util.List;
public class Triangle {
      private
List<Point> points;
      public void draw(){
            for(Point point:points){
                  System.out.println(point.getX()
+",
"+point.getY());
            }
      }
      public void
setPoints(List<Point> points) {
            this.points = points;
      }
      public
List<Point> getPoints() {
            return points;
      }
}
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="parentTriangle"
class="org.yash.watertechsol.Triangle" abstract="true">
            <property name="points">
                  <list>
                        <ref bean="pointA"
/>
                  </list>
            </property>
      </bean>     
      <bean id="triangle"
class="org.yash.watertechsol.Triangle" parent="parentTriangle">
            <property name="points">
                  <list merge="true">
                        <ref bean="pointB"
/>
                  </list>
            </property>       
      </bean>
      <bean id
="pointA"
class="org.yash.watertechsol.Point">
            <property name="x"
value="0" />
            <property name="y"
value="0" />
      </bean>
      <bean id
="pointB"
class="org.yash.watertechsol.Point">
            <property name="x"
value="-20" />
            <property name="y"
value="0" />
      </bean>
      <bean id
="pointC"
class="org.yash.watertechsol.Point">
            <property name="x"
value="0" />
            <property name="y"
value="20" />
      </bean>     
</beans>
*********************************************
Triangle.java
package org.yash.watertechsol;
public class Triangle {
      private Point pointA;
      private Point pointB;
      private Point pointC;
      public Point
getPointA() {
            return pointA;
      }
      public void setPointA(Point
pointA) {
            this.pointA = pointA;
      }
      public Point
getPointB() {
            return pointB;
      }
      public void setPointB(Point
pointB) {
            this.pointB = pointB;
      }
      public Point
getPointC() {
            return pointC;
      }
      public void setPointC(Point
pointC) {
            this.pointC = pointC;
      }
      public void draw(){
            System.out.println("Point A
= (" + getPointA().getX()+", "+getPointA().getY()+")");
            System.out.println("Point B
= (" + getPointB().getX()+", "+getPointB().getY()+")");
            System.out.println("Point C
= (" + getPointC().getX()+", "+getPointC().getY()+")");
      }
}
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="parentTriangle"
class="org.yash.watertechsol.Triangle">
            <property name="pointA"
ref="pointA"></property>
      </bean>     
      <bean id="triangle"
class="org.yash.watertechsol.Triangle" parent="parentTriangle">
            <property name="pointB"
ref="pointB"></property>
            <property name="pointC"
ref="pointC"></property>
      </bean>
      <bean id
="pointA"
class="org.yash.watertechsol.Point">
            <property name="x"
value="0" />
            <property name="y"
value="0" />
      </bean>
      <bean id
="pointB"
class="org.yash.watertechsol.Point">
            <property name="x"
value="-20" />
            <property name="y"
value="0" />
      </bean>
      <bean id
="pointC"
class="org.yash.watertechsol.Point">
            <property name="x"
value="0" />
            <property name="y"
value="20" />
      </bean>     
</beans>
No comments:
Post a Comment