Home » Spring » Autowiring in Spring

Autowiring in Spring

In Spring usually we provide bean configuration details in the spring bean configuration file and specify the beans that will be injected in other beans using ref attribute or using an inner bean.Spring framework provides autowiring features where we don’t need to provide bean injection details explicitly and spring container will take care about injecting the dependencies.

By default autowiring is disabled in spring framework and programmer has to explicitly wire the bean properties into an xml file.If autowiring is enabled then spring container will take care about injecting the dependencies, and no need to configure into an xml file explicitly.

Autowiring can't be used to inject primitive and string values, it will only supported if the dependancies are in the form of objects.

To enable Autowiring, just define the "autowire" attribute in <bean>.

<bean id="employee" class="com.jwt.spring.Employee" autowire="byName"/>

Autowiring Modes:

There are five autowiring modes available in Spring.

  • no - It is the default autowiring mode. It means no autowiring by default.
  • byName- The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method.
  • byType-The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.
  • constructor-The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
  • autodetect-It is deprecated since Spring 3.

Example:

We are going to create two java classes Department and Employee for demonstration of all the modes mentioned above.

Department.java

package com.jwt.spring;
public class Department {
	private Employee employee;
	public Department(Employee employee) {
		this.employee = employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	//..
}

Employee.java

package com.jwt.example;
public class Employee {
//..
}

1. Auto-Wiring 'no'

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

Employee.java

<bean id="Department" class="com.jwt.spring.Department">
	<property name="employee" ref="employee" />
</bean>
<bean id="employee" class="com.jwt.spring.Employee" />

2. Auto-Wiring 'byName'

The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same.In this case, since the name of "employee" bean is same with the name of the "Department" bean’s property ("employee"), so, Spring will auto wired it via setter method – "setEmployee(Employee employee)".

<bean id="department" class="com.jwt.spring.Department" autowire="byType"/>
<bean id="employee" class="com.jwt.spring.Employee" />

Refer Spring Autowiring by Name Tutorial for more details and working example.

3. Auto-Wiring 'byType'

The byType mode bean looks for other beans with the same type as property.For instance in our demo code 'Department' class has 'employee' variable, then configuring autowire=byType on 'Department' bean makes the container look for the beans of 'Employee' class and inject into 'Department' bean.Spring will auto wired it via setter method.

<bean id="department" class="com.jwt.spring.Department" autowire="byType" />
<bean id="employee" class="com.jwt.spring.Employee" />

Refer Spring Autowiring by Type Tutorial for more details and working example.

4. Auto-Wiring 'constructor'

Autowiring by constructor is similar to byType,but in byType we used setter injection here we have to use constructor injection.The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.

In this case, since the data type of "employee" bean is same as the constructor argument data type in "department" bean’s property (Employee object), so, Spring auto wired it via constructor – "Department(Employee employee)".

<bean id="department" class="com.jwt.spring.Department" autowire="constructor" />
<bean id="employee" class="com.jwt.spring.Employee" />

Refer Spring Autowiring by Constructor. Tutorial for more details and working example.

5. Auto-Wiring 'autodetect' (Deprecated since Spring 3)

It is deprecated since Spring 3.


Previous Next Article
comments powered by Disqus