Bean Autowiring in Spring Application

There are many collaborating bean in Spring for develop an application, the autowire help in making the relationship between them. Bean Autowiring reduces the effort of writing properties or constructor arguments. Bean Autowiring is the feature provided by the spring framework to skip the some of configuration in XML file. The bean autowiring in specified at the autowire attribute inside <bean></bean> element.

<bean autowire="byName" class="com.dineshonjava.sdnext.beanautowiring.Triangle" id="triangle"></bean>

 

Bean Autowiring in Spring

 

Bean Autowiring Modes

There are five bean autowiring modes which can be used to instruct Spring container to use bean autowiring for dependency injection. In above syntax we have used the autowire attribute of the element to specify autowire mode for a bean definition.

 Mode Explanation
no It is default which define no autowiring.
byName Autowiring is done by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.
byType Autowiring is done by matching data type of property name. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown.
constructor Autowiring is done by matching data type of property name with the property constructor argument. i.e. Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
autodetect When default constructor with no argument, it auto-wire by data type or it auto-wire by constructor means that Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType..

1. Auto-Wiring “no”

This is the default mode, you need to wire your bean via ‘ref’ attribute.
<bean class="com.dineshonjava.sdnext.autowiring.Circle" id="circle">
   <property name="center" ref="center">
</property></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>

2. Auto-Wiring “byName”

Auto-wire a bean by property name. In this case, since the name of “center” bean is same with the name of the “circle” bean’s property (“center”), so, Spring will auto wired it via setter method – “setCenter(Point center)”.
<bean autowire="byName" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>

Circle.java

public class Circle{
    private Point center;
    --
    public void setCenter(Point center){
       this.center = center;
    }

Click for see the full Example with Triangle class.

3. Auto-Wiring “byType”

Auto-wire a bean by property data type. In this case the data type of “center” bean is same with the data type of the “circle” bean’s property (“center”), so, Spring will auto wired it via setter method – “setCenter(Point center)”.
<bean autowire="byType" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>

Circle.java

public class Circle{
    private Point center;
    --
    public void setCenter(Point center){
       this.center = center;
    }

Click for see the full Example with Circle class.

4. Auto-Wiring “constructor”

Auto-wire a bean by property data type in constructor argument. In this case, since the data type of “center” bean is same as the constructor argument data type in “circle” bean’s property (Point center), so, Spring auto wired it via constructor method – “public Circle(Point center)”.
<bean autowire="constructor" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>

Circle.java

public class Circle{
    private Point center;
    --
    public Center(Point center){
       this.center = center;
    }

Click for see the full Example with Circle class.

5. Auto-Wiring “autodetect”

If a default constructor is found, uses “constructor”; Otherwise, uses “byType“. In this case, since there is a default constructor in “Circle” class, so, Spring auto wired it via constructor method – “public Circle(Point center)”.
<bean autowire="autodetect" class="com.dineshonjava.sdnext.autowiring.Circle" id="circle"></bean>
<bean class="com.dineshonjava.sdnext.autowiring.Point" id="center"></bean>

Circle.java

public class Circle{
    private Point center;
    --
    public Center(Point center){
       this.center = center;
    }

Click for see the full Example with Circle class.

Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing to developers to use it to wire only one or two bean definitions. Though, autowiring can significantly reduce the need to specify properties or constructor arguments but you should consider the limitations and disadvantages of autowiring before using them. Following limitations are
  • confusing nature
  • primitive data type
  • overriding possibility

Spring Related Topics you may like

 

 

Previous
Next

3 Comments

  1. Anonymous June 27, 2012
  2. Anonymous July 2, 2013
  3. Anonymous October 23, 2013