How does Spring Container Perform Dependency Injection?
In the earlier article, we have seen what is dependency injection and in what ways we can perform dependency injection. Now we will be moving forward to learn how dependency injection is happening internally.
We have already seen the spring container configuration part, we can configure the spring container in 4 ways:
1. XML based
2. Annotation based
3. Pure Java code base
4. Spring boot Auto driven
Here I will take an example of XML-based configuration and show how dependency injection is done.
This is the XML file I have for my application.
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
<constructor-arg ref="course"/>
</bean>
</beans>
The application has the main method as shown below.
public class TestApp {
public static void main(String[] args) {
ApplicationContext applicationContext
= new ClassPathXmlApplicationContext("src/applicationContext.xml");
Object object = applicationContext.getBean("university");
University university = (University) object;
System.out.println(university.printMessage("Virat", 12));
}
}
Now let us check what's happening in the backend and how spring doing dependency injection.
- Java starts the execution of the application from the main method.
- It comes are load the XML file for all the bean information.
ApplicationContext applicationContext
= new ClassPathXmlApplicationContext("src/applicationContext.xml
3. Then it comes to the next line. In this line, we are asking the spring container to get the bean name university.
Object object = applicationContext.getBean("university");
Then Spring container will check in the XML file for which class it needs to create a bean which is named university.
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
<constructor-arg ref="course"/>
</bean>
In the above XML file spring container found the bean with the id university and for which class src.University.
But as per our configuration, we are injecting other beans also into this bean so, now the spring container will look for beans named student and course as they are required for university bean creation.
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student"/>
<bean id="course" class="src.model.Course"/>
Spring container found the bean configuration for the student and course bean in the configuration file so, spring container will create the beans for this and store them in (Cache) HashMap with key as bean name and value as the class name for future reference.
Now as the dependent bean objects are ready in the container, the spring container will go and create the university bean.
After the university bean creation is done the container will have all the information of beans for future reference so that the container can use whenever required. Finally, we got an object from line 2 in the main method.
4. As we got the object we will case to the required type in our code, it is the University type.
University university = (University) object;
5. As we got the required type object we will use this to make make a call to that method which contains business logic
System.out.println(university.printMessage("Virat", 12));
Finally, after executing the business logic java execution comes out of the main method and stops the application.
Please do follow for more articles and learn with me.
Special Thanks to Nitin M who made these all concepts very easy.