Friday, 3 February 2012

Autowiring in Spring

 So far you have seen how to wire all of your bean's properties using either the <constructor-arg> or the <property> element. In large application, however, all or this explicit wiring can result into lot of xml code. Rather than explicit wiring all of your bean's properties, you can have Spring automatically figure out how to write beans together by setting the autowire property on each <bean> that you want Spring to autowire.

Spring provides four types of autowiring

  • byName
  • byType
  • constructor
  • autodetect


Autowiring byName 
Attempts to find a bean in the container whose name (or Id ) is the same as the name of property being wired. If a matching bean is not found, the property will remain unwired.

Example

DrawingApp.java

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 

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 : ("+pointA.getX()+","+pointA.getY()+")" );
        System.out.println("Point B : ("+pointB.getX()+","+pointB().getY()+")" );
        System.out.println("Point C : ("+pointC.getX()+","+pointC().getY()+")");
    }
}

Point.java

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//DTDBEAN2.0//EN"  "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="triangle" class="com.example5.Triangle" autowire="byName" />
   
    <bean id="pointA" class="com.example5.Point">
        <property name="x" value="0"/>
        <property name="y" value="0"/>
    </bean>
      <bean id="pointB" class="com.example5.Point">
        <property name="x" value="-20"/>
        <property name="y" value="0"/>
    </bean>
    <bean id="pointC" class="com.example5.Point">
        <property name="x" value="20"/>
        <property name="y" value="0"/>
    </bean>
</beans>

Output : Point A : (0,0)
                 Point B : (-20,0)
                Point C : (20,0)


 Autowiring byType

Attempts to find a single bean in the container whose type matches the type of the property being  wired. If a matching bean is not found, the property will remain unwired. If more than one bean matched found, then UnsatisfiedDependencyException will be thrown.

Example :

DrawingApp.java

public class  DrawingApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        Circle circle = (Circle) context.getBean("circle");
        circle.draw();
    }
}

Circle.java

public class Circle{

    private Point center;

    public Point getCenter() {
        return pointA;
    }
    public void setCenter(Point center) {
        this.center = center;
   }
    public void draw(){
        System.out.println(" Center Point is  : ("+center.getX()+","+center.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="circle" class="com.example5.Triangle" autowire="byType" />
    <bean id="pointA" class="com.example5.Point">
        <property name="x" value="0"/>
        <property name="y" value="0"/>
    </bean>
</beans>

Output : Center Point is (0,0)

Autowiring By Constructor

Tries to match up one or more beans in the container with the parameters of one of the constructors of the bean being wired. In the event of ambiguous beans or constructor, UmsetisfiedDependencyException will be thrown.

Example :

DrawingApp.java  

public class  DrawingApp {
    public static void main(String[] args) {  
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        Circle circle = (Circle) context.getBean("circle");        
        circle .draw();
    }
}

Circle.java

public class Circle{

    private Point center;
    public Triangle(Point center){
        this.center = center;
    }
    public void draw(){
        System.out.println("Center Point is ("+center.getX()+","+center.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="circle" class="com.example5.Triangle" autowire="constructor" />
   
    <bean id="pointA" class="com.example5.Point">
        <property name="x" value="0"/>
        <property name="y" value="0"/>
    </bean>
</beans>

Output : Center Point is (0,0)




No comments:

Post a Comment