Autowiring In Spring

 

Introduction

Spring provides a way to automatically detect the relationships between various beans. This can be done by declaring all the bean dependencies in Spring configuration file. So, Spring is able to utilize the BeanFactory to know the dependencies across all the used beans.

The XML configuration based autowiring functionality has five modes – 

no
byName
byType
constructor
, and 
autodetect
. The default mode is 
no
.

Autowiring Modes

Spring supports the following autowiring modes:

    • no: It’s the default autowiring mode. It means 
      no
       autowiring.
    • byName: The 
      byName
       mode injects the object dependency according to name of the bean. In such case, property name and bean name should be same. It internally calls setter method.
    • byType: The 
      byType
       mode injects the object dependency according to type. So it can have different property name and bean name. 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: In this mode, Spring first tries to autowire by 
      constructor
      . If this fails, it tries to autowire by using 
      byType
      .

1. Autowiring ‘no’:

This is a default autowiring mode. It means 

no
 autowiring.

<bean id="department" class="guru.springframework.autowiringdemo.Department">
<property name="deptName" value="Information Technology" />
</bean>
<bean id="employee" class="guru.springframework.autowiringdemo.Employee"></bean>

2. Autowiring ‘byName’:

This option enables autowire based on bean names. Spring looks up the configuration file for a matching bean name. If found, this bean is injected in the property. However, if no such bean is found, an error is raised.

In this case, the name of the 

department
 bean is same as the 
employee
 bean’s property (
Department
), so Spring will be autowired to it via the setter method – 
setDepartment(Department department)
.

<bean id="department" class="guru.springframework.autowiringdemo.Department">
<property name="deptName" value="Information Technology" />
</bean>
<bean id="employee" class="guru.springframework.autowiringdemo.Employee" autowire="byName"></bean>

3. Autowiring ‘byType’:

This option enables the autowire based on bean type. It searches property’s class type in configuration file. It injects the property, if such bean is found, otherwise an error is raised.

In this case, the data type of the 

department
 bean is same as the data type of the 
employee
 bean’s property (
Department
 object), therefore Spring will autowire it via the setter method – 
setDepartment(Department department)
.

<bean id="department" class="guru.springframework.autowiringdemo.Department">
<property name="deptName" value="Information Technology" />
</bean>
<bean id="employee" class="guru.springframework.autowiringdemo.Employee" autowire="byType"></bean>

4. Autowiring ‘constructor’

Autowiring by 

constructor
 is similar to 
byType
 but it applies to constructor arguments. It will look for the class type of constructor arguments, and then do an autowire 
byType
 on all constructor arguments. If exactly one bean of the constructor argument type is not present in the container, a fatal error will be raised.

The data type of 

department
 bean is same as the constructor argument data type in 
employee
 bean’s property (
Department
 object). Therefore, Spring autowires it using the constructor method – 
public Employee(Department department)
.

<bean id="department" class="guru.springframework.autowiringdemo.Department">
<property name="deptName" value="Information Technology" />
</bean>
<bean id="employee" class="guru.springframework.autowiringdemo.Employee" autowire="constructor"></bean>

5. Autowiring ‘autodetect’

Autowiring by 

autodetect
 uses two modes i.e. 
constructor
 or 
byType
 modes. First, it will look for valid constructor with arguments. If it is found then the 
constructor
 mode is chosen. If there is no constructor defined in a bean, the autowire 
byType
 mode is chosen.

In following case, since there is a 

Department
 object in the 
Employee
 class, Spring autowires it using 
byType
 via the setter method – 
setDepartment(Department department)
.

<bean id="department" class="guru.springframework.autowiringdemo.Department">
<property name="deptName" value="Information Technology" />
</bean>
<bean id="employee" class="guru.springframework.autowiringdemo.Employee" autowire="autodetect"></bean>

Note : Autodetect functionality will work with the 2.5 and 2.0 schemas. It will not work from 3.0+.

Example of Autowiring

We’ll create a simple Java Bean, named 

Department
Department
 will have department name property with getter and setter methods. After that, we will initialize this property value in the Spring bean configuration file.

public class Department {
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}

Now let’s create our 

Employee
 class. In which we will inject 
Department
 bean through Spring autowiring.

public class Employee {
private int eid;
private String ename;
private Department department;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public void showEployeeDetails(){
System.out.println("Employee Id : " + eid);
System.out.println("Employee Name : " + ename);
System.out.println("Department : " + department.getDeptName());
}
}

Now, looking at Spring bean configuration file, it is the main part of any Spring application. So let’s see how our Spring bean configuration file looks.

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="department" class="guru.springframework.autowiringdemo.Department">
<property name="deptName" value="Information Technology" />
</bean>
<bean id="emp" class="guru.springframework.autowiringdemo.Employee" autowire="byName"></bean>
</beans>

Now, our Spring application is ready with all types of Spring autowiring. So let’s write a simple test program to see if it works as expected.

@SpringBootApplication
public class AutowiringdemoApplication {
public static void main(String[] args) {
SpringApplication.run(AutowiringdemoApplication.class, args);
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = context.getBean("employee", Employee.class);
emp.setEid(101);
emp.setEname("Spring Framework Guru");
emp.showEployeeDetails();
}
}

In the above program, we are just creating the Spring application context and using it to get different beans and printing the employee details.

After we run the above program, we get the following output:

Employee Id : 101
Employee Name : Spring Framework Guru
Department : Information Technology
Process finished with exit code 0

@Autowired Annotation

In Spring, you can use 

@Autowired
 annotation to auto wire bean on the setter method, constructor or a field. Moreover, it can autowire property in a particular bean. We must first enable the annotation using below configuration in configuration file.

If you are using Java based configuration, you can enable annotation-driven injection by using below spring configuration:

@Configuration
@ComponentScan("guru.springframework.autowiringdemo")
public class AppConfig {}

As an alternative, we can use below XML based configuration in Spring:

<context:annotation-config />

We have enabled annotation injection. After that, it can be used on modes like properties, setters, and constructors. Let’s discuss them one by one.

@Autowired on Properties

In the below example, when the annotation is directly used on properties, Spring looks for and injects 

Department
 when 
Employee
 is created. This is how it eliminates the need for getters and setters.

import org.springframework.stereotype.Component;
@Component
public class Department {
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
private int eid;
private String ename;
@Autowired
private Department department;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public void showEployeeDetails(){
System.out.println("Employee Id : " + eid);
System.out.println("Employee Name : " + ename);
department.setDeptName("Information Technology");
System.out.println("Department : " + department.getDeptName());
}
}

@Autowired on Setters

In the below example, when the annotation is used on the setter method, the setter method is called with the instance of 

Department
 when 
Employee
 is created.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Employee {
private int eid;
private String ename;
private Department department;
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Department getDepartment() {
return department;
}
@Autowired
public void setDepartment(Department department) {
this.department = department;
}
public void showEployeeDetails(){
System.out.println("Employee Id : " + eid);
System.out.println("Employee Name : " + ename);
department.setDeptName("Information Technology");
System.out.println("Department : " + department.getDeptName());
}
}

@Autowired on Constructors

In the below example, the annotation is used on a constructor, an instance of 

Department
 is injected as an argument to the constructor when 
Employee
 is created.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Employee {
private int eid;
private String ename;
private Department department;
@Autowired
public EmployeeBean(DepartmentBean deptBean) {
System.out.println("*** Autowiring by using @Autowire annotation on constructor ***");
this.deptBean = deptBean;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public void showEployeeDetails(){
System.out.println("Employee Id : " + eid);
System.out.println("Employee Name : " + ename);
department.setDeptName("Information Technology");
System.out.println("Department : " + department.getDeptName());
}
}

Writing a Test Program

So let’s write a simple test program for 

@Autowired
 on property to see if it works as expected.

@SpringBootApplication
public class AutowiringdemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(AutowiringdemoApplication.class, args);
Employee emp = context.getBean(Employee.class);
emp.setEid(104);
emp.setEname("Spring Framework Guru");
emp.showEployeeDetails();
}
}

After we run the above program, we get the following output:

Employee Id : 104
Employee Name : Spring Framework Guru
Department : Information Technology
Process finished with exit code 0

Autowire Conflict Resolution

By default, Spring resolves 

@Autowired
 entries 
byType
. If more than one beans of the same type are available in the container, the framework will throw 
NoUniqueBeanDefinitionException
 exception indicating that more than one bean is available for autowiring. Please click here to know more on how to fix 
NoUniqueBeanDefinitionException
 exceptions.

Summary

In this post, we’ve seen a few modes of the autowiring object using Spring ApplicationContext and Spring configuration file. We have looked at examples using different modes which are:

  • no
  • byName
  • byType
  • constructor
  • autodetect

We also saw a simple examples of autowiring using 

@Autowired
 annotation using different modes which are:

  • property
  • setter
  • constructor

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence