Spring Autowiring

 Autowiring feature of spring framework allows to inject the object dependency implicitly. It uses setter or constructor injection internally.

Autowiring feature can’t be used to inject primitive values. It works with referenced values only.

Autowiring Modes

  • no : This is default autowiring mode for spring framework and it means that autowiring is disabled.
  • byName : This autowiring  mode enables the dependency injection based on bean names. When autowiring a property in bean, property name is used for searching a matching bean definition in configuration file.
  • byType : This autowiring  mode enables the dependency injection based on bean types. When autowiring a property in bean, property’s class type is used for searching a matching bean definition in configuration file. Autowiring byType will result in runtime error if more than one bean of a property type are configured.
  • constructor : Autowiring by constructor is similar to byType, but applies to constructor arguments. 
  • autodetect : Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. It first looks for valid constructor with arguments, If found the constructor mode is used. If there is no constructor defined in bean, or explicit default no-args constructor is present, the autowire byType mode is used.

Here I am taking example to autowire address in customer bean using byName,byType and constructor.

Customer.java

package com.startwithjava;

public class Customer {
	private int id;
	private String name;
	private Address address;
	
	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Customer(int id, String name, Address address) {
		super();
		this.id = id;
		this.name = name;
		this.address = address;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public void display(){
		System.out.println("Name:"+name+"\tCity:"+address.getCity()+"\tState"+address.getState());
	}
}

Address.java

package com.startwithjava;
public class Address {
	private String state;
	private String city;
	private String zipCode;
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
}

applicationContext.xml

<bean> element has property autowire.Here I am using byName,byType and constructor as value for this attribute.

<?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="address" class="com.startwithjava.Address">
   <property name="city" value="New Delhi" />
   <property name="state" value="New Delhi" />
   <property name="zipCode" value="110092" />
 </bean>
  <bean id="cust" class="com.startwithjava.Customer" autowire="byName">
   <property name="id" value="1000"/>
   <property name="name" value="Ramesh"/>
 </bean>
  <bean id="cust1" class="com.startwithjava.Customer" autowire="byType">
   <property name="id" value="2000" />
   <property name="name" value="Raj" />
 </bean>
  <bean id="cust2" class="com.startwithjava.Customer" autowire="constructor">
  </bean>
 </beans> 

Test.java

package com.startwithjava;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test { 
public static void main(String[] args) { 
    Resource r=new ClassPathResource("applicationContext.xml"); 
    BeanFactory factory=new XmlBeanFactory(r); 
    Customer c  = (Customer)factory.getBean("cust");
    Customer c1  = (Customer)factory.getBean("cust1");
    Customer c2  = (Customer)factory.getBean("cust2");
    c.display();
    c1.display();
    c2.display();
} 
} 

Leave a Reply