Monday, July 22, 2013

19-The-Autowired-Annotation

Autowire first look for type if there is only type available in XML then it is going to autowire with that type because there is only one bean – even though ID is different. If XML has beans of same type more than one time then it will through an exception. In this case you need to configure bean ID which matches with setter method type.
Eg.
      <bean id="circle" class="org.yash.watertechsol.Circle">
      </bean>
     
      <bean id ="center" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="0" />
      </bean>
      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
      </bean>

      @Autowired
      public void setCenter(Point center) {
            this.center = center;
      }


Suppose if you are not able to change bean id because it is used by some other resources, then you need to go with @Qualifer annotation below changes need to do.
1.       Remove DocType from xml
2.       Add below code
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

      <bean id ="pointA" class="org.yash.watertechsol.Point">
            <qualifier value="circleRelated"></qualifier>
            <property name="x" value="0" />
            <property name="y" value="0" />
      </bean>

      <bean id="circle" class="org.yash.watertechsol.Circle">
      </bean>
      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
      </bean>

      @Autowired
      @Qualifier("circleRelated")
      public void setCenter(Point center) {
            this.center = center;
      }

Note: Instead of adding all the annotation beans in XML you can configure all annotation using below config.
      <context:annotation-config />
            Or
      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
      </bean>

DrawingApp.java
package org.yash.watertechsol;

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


public class DrawingApp {
      public static void main(String[] args){
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            context.registerShutdownHook();
            Shape shape = (Shape)context.getBean("circle");
            shape.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;


public class Triangle implements Shape{
     
      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("Drawing Triangle");
            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()+")");
      }

}

Shape.java
package org.yash.watertechsol;

public interface Shape {
      public void draw();
}

Circle.java
package org.yash.watertechsol;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Circle implements Shape {

      private Point center;

      @Override
      public void draw() {
            System.out.println("Drawing Circle");
            System.out.println("("+center.getX()+", "+center.getY()+")");
      }
     
      public Point getCenter() {
            return center;
      }
     
      @Autowired
      @Qualifier("circleRelated")
      public void setCenter(Point center) {
            this.center = center;
      }
}

spring.xml
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           
      <!-- <bean id="triangle" class="org.yash.watertechsol.Triangle">
            <property name="pointA" ref="pointA"></property>
            <property name="pointB" ref="pointB"></property>
            <property name="pointC" ref="pointC"></property>
      </bean> -->

      <bean id="circle" class="org.yash.watertechsol.Circle">
      </bean>
     
      <bean id ="pointA" class="org.yash.watertechsol.Point">
            <qualifier value="circleRelated"></qualifier>
            <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>
     
      <!-- <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"></bean> -->
      <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
      </bean>
</beans>





1
@Required
The @Required annotation applies to bean property setter methods.
2
@Autowired
The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.
3
@Qualifier
The @Qualifier annotation along with @Autowired can be used to remove the confusion by specifiying which exact bean will be wired.
4
JSR-250 Annotations
Spring supports JSR-250 based annotations which include @Resource, @PostConstruct and @PreDestroy annotations.


No comments:

Post a Comment