Tuesday, April 24, 2012

The Autowired Annotation


we'll use the Autowired annotation to wire up dependencies. We'll learn how to add dependencies by type and name. We'll also use Qualifiers to narrow down dependency contenders.



org.springframework.beans.factory.annotation.Autowired


Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.
Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.
Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.
In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.
In case of a Collection or Map dependency type, the container will autowire all beans matching the declared value type. In case of a Map, the keys must be declared as type String and will be resolved to the corresponding bean names.
Note that actual injection is performed through a BeanPostProcessor which in turn means that you cannot use @Autowired to inject references into BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).


*************************************


Open Declaration org.springframework.beans.factory.annotation.Qualifier


This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers.


******************************** 



package com.venkat.core;


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


public class Circle implements Shape {


private Point center;

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

package com.venkat.core;

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


public class DrawingApp {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");
Shape shape=(Shape)factory.getBean("circleBean");
//Shape shape=(Shape)factory.getBean("triangleBean");
shape.draw();

}

}
***********************************************************************
package com.venkat.core;

public class Point {

private int x;
private int y;
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}

}
***********************************************************************
package com.venkat.core;

public interface Shape {
public void draw();
}
***********************************************************************
package com.venkat.core;


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

}
***********************************************************************
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="triangleBean" class="com.venkat.core.Triangle" >
<property name="pointA" ref="zeroPoint"/>
<property name="pointB" ref="point1"/>
<property name="pointC" ref="point2"/>
</bean>
<bean id="zeroPoint" class="com.venkat.core.Point">
<qualifier value="circleRelated" />
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>
<bean id="point1" class="com.venkat.core.Point">
<property name="x" value="10"/>
<property name="y" value="10"/>
</bean>
<bean id="point2" class="com.venkat.core.Point">
<property name="x" value="0"/>
<property name="y" value="10"/>
</bean>
<!-- <bean id="center" class="com.venkat.core.Point"> -->
<!-- <property name="x" value="30"/> -->
<!-- <property name="y" value="10"/> -->
<!-- </bean> -->
<bean id="circleBean" class="com.venkat.core.Circle" >
<!-- <property name="center" ref="zeroPoint"/>  -->
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> -->
</beans>

No comments:

Post a Comment