Dependency Injection in Spring

Rod Johnson created Spring Framework. Spring framework is very famous for Dependency Injection.  Before going to learn about Spring and its concepts, we will first understand what Dependency Injection in Spring is.

Related Spring Tutorials

How to Create Spring MVC application using Java Based Configuration

Generate Excel in Spring MVC Application using Apache POI

JQuery Datatable in Spring MVC Application

Advance Features of JQuery Datatable in Spring MVC Application

Dependency Injection in Spring

            We all have Smart Phones, in which we must have Gallery Application. Let’s consider Gallery Application as a Class Gallery (Dependency Injection Figure 1.1).

Dependency Injection in Spring

Dependency Injection in Spring 1.1

We know in all Gallery Application we can send/share our photos/videos to others. In order to share photos/videos we will use some other application like WatsApp or Gmail. So Our Gallery Application having the dependency of WatsApp or Gmail (Dependency Injection Figure 1.2)

Dependency Injection in Spring

Dependency Injection in Spring 1.2

 

If we want to share photos using some other external application like Gmail, WeChat, etc then we have to create all the all the Other class instances inside Gallery (Fig 1.3). So instance of WatsApp, WeChat or Gmail inside Gallery class makes the Gallery class tight coupled.

Dependency Injection in Spring

Dependency Injection in Spring 1.3

Using Dependency Injection we can make the Gallery class as loosely coupled. In order to make Dependency Injection, first we have to make a Contract called Interface for All the External Application class i.e WatsApp, WeChat, Gmail.  For the interface we have to create implementations  as mentioned  figure 1.4.

Dependency Injection in Spring

Dependency Injection in Spring – 1.4

In Gallary class instead of directly creating an instance of External Application class (WatApp, Gmail, WeChat), we have created it and assigned to Interface (SharingApp), this concept is called RunTime Polymorphishm (Fig 1.5). With the help of Run Time polymorphism we can pass the SharingApp instance to any method which is of type SharingApp. The corresponding implementation will be executed at run time.

Dependency Injection in Spring

Dependency Injection in Spring – 1.5

But still our gallery class is still tightly coupled with External Application class, in order to eliminate the dependency of External Application in Gallery class, the External Application dependency has to be injected (passed) dynamically.

In order to inject dependency, we should have a setter/constructor which accepts our dependency as a parameter and assigned to our dependency variable.(Fig 1.6) So during run time we can pass our dependencies and inject it to our gallery class. In this way even if the external application grows (like FB Messenger, Hike etc), we can inject it to our gallery class dynamically.  

Dependency Injection in Spring

Dependency Injection in Spring

Spring Container will take care of dependency creation and dependency injection, which we will see in fore coming tutorials.

To summarize, We have created an Interface/Abstract Class and kept it as member variable in class. We also provided a constructor or setter method which is of Interface type. During run time the corresponding implementation class will be instantiated and passed (injected) to setter/constructor, through which we have reduced the class from tightly coupled to loosely coupled.

 

Leave a comment

Your email address will not be published. Required fields are marked *