Friday, October 2, 2015

Spring Framework- 9 - autowiring

Autowiring is a technique of the spring framework to automate the initiation of the bean Class variables without actually defining them within the bean tags.
(also refer autowiring performed using Annotations, in the later links)

Let's say, class Triangle with 3 Point variables as below,

public class Triangle {

public 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("PointA: (" + getPointA().getX() + "," + getPointA().getY()+")");
System.out.println("PointB: (" + getPointB().getX() + "," + getPointB().getY()+")");
System.out.println("PointC: (" + getPointC().getX() + "," + getPointC().getY()+")");
}
}

The respective spring.xml would be as below:

<beans>
<bean id="triangle" class="com.product.springDemo.Triangle">
<property name="pointA" ref="Bean-pointA"/>
<property name="pointB" ref="Bean-pointB"/>
<property name="pointC" ref="Bean-pointC"/>
</bean>
<bean id="Bean-pointA" class="com.product.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>
<bean id="Bean-pointB" class="com.product.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>
<bean id="Bean-pointC" class="com.product.springDemo.Point">
<property name="x" value="30"/>
<property name="y" value="300"/>
</bean>
</beans>

However, using autowire makes it relatively simpler as below,



All we need to do is to just add a "autowire" attribute to the <bean> tag.
autowire attribute has 3 values:

  1. byName - tries to find a match of the Triangle class variable with each of the defined <bean id=" attribute. and if there's a match, the variable is initialised.
  2. constructortries to find a match of the Triangle class Contructor parameters variable with each of the defined <bean id=" attribute. and if there's a match, the variable is initialised.
  3. byTypetries to find a match of the Triangle class variable with each of the defined <bean class=" attribute. and if there's a match, the variable is initialised.
  4. default
  5. Autodetect
  6. no -  autowiring is deactivated
<beans>
<bean id="triangle" class="com.findgoose.springDemo.Triangle" autowire="byName ">
</bean>
<bean id="pointA" class="com.product.springDemo.Point">
<property name="x" value="10"/>
<property name="y" value="100"/>
</bean>

<bean id="pointB" class="com.product.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>

<bean id="pointC" class="com.product.springDemo.Point">
<property name="x" value="30"/>
<property name="y" value="300"/>
</bean>
<alias name="triangle" alias="threeSided"/>
</beans>

No comments:

Post a Comment