Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Spring - Autowiring by Name & Type

Let us define the 'Car' and 'Wheel' to understand Autowiring in detail:


Car Class

class Car {

 Wheel wheel;

 public Wheel getWheel() {
   return wheel;
 }

 public void setWheel(Wheel wheel) {
   this.wheel = wheel;
 }
}


Wheel Class

class Wheel {

 String tireType;

 public String getTireType() {
   return tireType;
 }

 public void setTireType(String tireType) {
   this.tireType = tireType;
 }

}


Autowiring by Name

Let us understand 'Autowiring by Name' by looking at 'application.xml'.

application-context.xml

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
 xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation = "http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean name = "car" class = "Car" autowire="byName">
   <!-- We have removed the below line.
   <property name = "wheel" ref = "wheelReference"/>
   -->
 </bean>

 <bean name = "wheel" class = "Wheel">
   <property name = "wheel" value="Thick Tire"/>
 </bean>

</beans>

Now, if we compare this 'application-context.xml' with the 'application-context.xml' defined in the previous page, we can note the below changes:

1. We have added 'autowire="byName"' to the <bean ..> of 'Car'.

<bean name = "car" class = "Car" autowire="byName">

2. And removed

<property name = "wheel" ref = "wheelReference"/>

3. And most importantly changed the <bean name=...> from 'wheelReference'

<bean name = "wheelReference" class = "Wheel">

to <bean name=...> as 'wheel'.

<bean name = "wheel" class = "Wheel">

How does Autowiring by Name happens?
<bean name = "car" class = "Car" autowire="byName">

By using the above code we are asking Spring to check for the variable names in the 'Car' class:


class Car {
  Wheel wheel;
}

When it finds there is a variable named 'wheel', it starts to search in 'application-context.xml' with '<bean name=...>' as 'wheel'.


And once it finds there is a bean with name 'wheel',


<bean name = "wheel" class = "Wheel">

It injects the 'wheel' object to the 'Car' class using the setter method of 'wheel' in 'Car' class.


public void setWheel(Wheel wheel) {
  this.wheel = wheel;
}

Autowiring by Type

Autowiring by Type is also similar to Autowiring by Name with a one liner change.


Instead of writing autowire="byName":


<bean name = "car" class = "Car" autowire="byName">

Just change it to autowire="byType"


<bean name = "car" class = "Car" autowire="byType">

and rest all remains the same.


How does Autowiring by Type happens?

<bean name = "car" class = "Car" autowire="byType">

By using the above code we are asking Spring to check for the data types in the 'Car' class:


class Car {
  Wheel wheel;
}

When it finds there is a variable of type 'Wheel', it starts to search in 'application-context.xml' with '<bean ... class=...>' as 'Wheel'.


And once it finds there is a bean with class as 'Wheel',


<bean name = "wheel" class = "Wheel">

It injects the 'wheel' object to the 'Car' class using the setter method of 'wheel' in 'Car' class.


public void setWheel(Wheel wheel) {
  this.wheel = wheel;
}

Note : Autowiring by Type cannot happen if there are two or more variables of the same datatype. i.e. If 'Car' class contains two variables of type'Wheel', Autowiring by Type does not work.