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




Spring - Autowiring using XML

The application-context.xml can have a lot of beans defined inside it referencing each other.


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">
   <property name = "wheel" ref = "wheelReference"/>
  </bean>

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

</beans >


In the above case a 'Wheel' object is created and injected to 'Car'. And the link happened with the 'ref' 'wheelReference'.


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


Now, what if we do not want to have the reference i.e. 'wheelReference' and let Spring figure out a different way to achieve this.

And thus Autowiring comes into picture.


Autowiring

Autowiring is a great feature which allows spring to get the reference objects by name or by type(Not to worry by the terms, we will explain in a while).


Types of Autowiring

1. Autowiring by Name

2. Autowiring by Type

3. Autowiring by Constructor