Monday, July 22, 2013

08-Inner-Beans-Aliases-and-idref

Inner beans can be define whenever you thing, the particular object do not have any more references.
Eg.
      <bean id="triangle" class="org.yash.watertechsol.Triangle">
            <property name="pointA">
                  <bean class="org.yash.watertechsol.Point">
                        <property name="x" value="0" />
                        <property name="y" value="0" />
                  </bean>          
            </property>
            <property name="pointB" ref="point2" />        
            <property name="pointC" ref="point3" />
      </bean>
Here you do not need to mention any ID to bean because it is no where used and property also do not have ref value.

Alias can be define using below configuration
      <alias name="triangle" alias="triangle-alias"/>

Alias can also be done using be config.
<bean id="triangle"
class="org.yash.watertechsol.Triangle"
name="triange-name">

If you want to access bean with only by ID then below config
      <idref bean = “PointA”> or <ref bean = “PointA”>
ref can point to name, id or alias
If you want to restrict ref to only it’s then use idref.

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("triange-name");
            triangle.draw();
      }
}


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()+")");
      }
}

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" name="triange-name">
            <property name="pointA">
                  <bean class="org.yash.watertechsol.Point">
                        <property name="x" value="0" />
                        <property name="y" value="0" />
                  </bean>          
            </property>
            <property name="pointB">
                  <ref bean="point3" />
            </property>      
            <property name="pointC" ref="point3" />
      </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>    

      <alias name="triangle" alias="triangle-alias"/>

</beans>

No comments:

Post a Comment