Friday 3 January 2014

Spring Auto-Wiring Beans

In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in <bean>.



Autowiring Modes:

There are following autowiring modes which can be used to instruct Spring container to use autowiring for dependency injection. You use the autowire attribute of the <bean/> element to specify autowire mode for a bean definition.

<bean id="customer" class="com.mahesh.training.spring.Customer" autowire="byName"/>
 
5 Auto-wiring modes are supported.
  1. no – This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter.
  2. byName – Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.
  3. byType – Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it.
  4. constructor – byType mode in constructor argument.
  5. autodetect – If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”. 

Examples:

 A Customer and Address object for auto wiring demonstration.

package com.mahesh.training.spring;

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


public class Customer {
   
    private Address address;

    public Customer(Address address ) {
         this.address = address;
     }   

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Customer [address=" + address + "]";
    }

}


package com.mahesh.training.spring;

public class Address {
 
 private String city;

 public String getCity() {
  return city;
 }

 public void setCity(String city) {
  this.city = city;
 }
 
 @Override
 public String toString() {
  return "Address [city=" + city + "]";
 }

}
 

Auto-Wiring ‘no’ :

This is the default mode, you need to wire your bean via ‘ref’ attribute.

<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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="customer" class="com.mahesh.training.spring.Customer">
        <property name="address" ref="address"></property>
    </bean>

    <bean id="address" class="com.mahesh.training.spring.Address">
        <property name="city" value="Bangalore"></property>
    </bean>

</beans>

Auto-Wiring ‘byType’ :

Auto-wire a bean by property data type. In this case, since the data type of “address” bean is same as the data type of the “customer” bean’s property (Address object), so, Spring will auto wired it via setter method

 <bean id="customer" class="com.mahesh.training.spring.Customer" autowire="byType"/>

 <bean id="address" class="com.mahesh.training.spring.Address">
      <property name="city" value="Bangalore"></property>
 </bean>

Auto-Wiring ‘byName’ :

 Auto-wire a bean by property name. In this case, since the name of “address” bean is same with the name of the “customer” bean’s property (“address”), so, Spring will auto wired it via setter method

 <bean id="customer" class="com.mahesh.training.spring.Customer" autowire="byName"/>

 <bean id="address" class="com.mahesh.training.spring.Address">
      <property name="city" value="Bangalore"></property>
 </bean>

Auto-Wiring ‘constructor’ :

Auto-wire a bean by property data type in constructor argument. In this case, since the data type of “address” bean is same as the constructor argument data type in “customer” bean’s property (Address object), so, Spring auto wired it via constructor method 

 <bean id="customer" class="com.mahesh.training.spring.Customer" autowire="constructor"/>

 <bean id="address" class="com.mahesh.training.spring.Address">
      <property name="city" value="Bangalore"></property>
 </bean>

Auto-Wiring ‘autodetect’:


If a default constructor is found, uses “constructor”; Otherwise, uses “byType”. In this case, since there is a default constructor in “Customer” class, so, Spring auto wired it via constructor method

  <bean id="customer" class="com.mahesh.training.spring.Customer" autowire="autodetect"/>

 <bean id="address" class="com.mahesh.training.spring.Address">
      <property name="city" value="Bangalore"></property>
 </bean>

 Note
It’s always good to combine both ‘auto-wire’ and ‘dependency-check’ together, to make sure the property is always auto-wire successfully.

  <bean id="customer" class="com.mahesh.training.spring.Customer" autowire="autodetect" dependency-check="objects"/>

 <bean id="address" class="com.mahesh.training.spring.Address">
      <property name="city" value="Bangalore"></property>
 </bean>






No comments:

Post a Comment