Dependency Injection

DI Introduction

Dependency Injection in Spring assists achieve low coupling between the applications object. Low coupling ensures very little dependency between various programming code that collaborate with each other to achieve some meaningful task in the application.


Advantages of DI

Dependency Injection in Spring has following advantages:

  1. Spring framework helps make our application code simple and comrehensive thus increases the readability.
  2. The code in Spring is easy to understand and maintain.
  3. As the programming codes in the application have low coupling among them so it becomes easy for the developers to write unit test case for each piece of code and test.

What is tightly coupled code?


Let's have an example to understand the same.

public class TightlyCoupled {

      private HelloWorld hWorld;
      public TightlyCoupled(){
            this.hWorld = new HelloWorld();

      public void diplayMessage(){



In the above code snippet, we can see that TightlyCoupled creates its own HelloWorld object inside the constructor. This is what makes "TightlyCoupled" class tightly coupled to the "HelloWorld class".

this.hWorld = new HelloWorld(); //tightly Coupled code inside constructor


Consequence of above coding strategy

  • This kind of coding increases the dependency on the other class to a great extent. As a result the class cannot perform the major tasks independently at all.
  • Writing unit test case becomes difficult as a result of such coding strategy.

So far we got to learn that tight coupling is not a good software engineering approach. Rather each piece of code should be functionally independent (highly-cohesive) which would decrease its dependency (low coupling) on other code in the application. At the same time we should keep in mind that two seperate code with no coupling at all will also be of no use at all. So there must be coupling (as limited as possible) but it should be well managed. 

Explained above is the task that DI does extremely well.


Working of DI in Spring

Using DI or dependency injection, the objects are provided with their dependencies at the time of their creation by some third party that co-ordinates with each object in the system. They are not expected to create or get their dependencies at all.


For now just have a look at the below given code:


public class LooselyCoupled {

      private HelloWorld hWorld;
      public LooselyCoupled(HelloWorld helloWorld){ //HelloWorld injected
            this.hWorld = helloWorld;
      public void diplayMessage(){


Isn't it much better than what we saw as a tightly coupled code?


Here "LooselyCoupled" class doesn't create its own "HelloWorld" object.

Rather it is provided with a helloWorld during construction time inside the constructor as a constructor argument. This is the concept of Dependency injection

Since the dependency is invoked as a constructor argument, so this kind of DI is known as constructor injection in spring. 


Another way to invoke dependency is using setter injection.


Types of DI

We have two ways to achieve dependency injection in Spring:

  1. Constructor Injection
  2. Setter Injection

We will learn each of them in the respective sections of this spring tutorial.


Please Share this page
Views : 152
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!