What is Dependency Injection and Inversion of Control in Spring Framework?

Inversion of control- It means giving the control of creating and instantiating the spring beans to the Spring IOC container and the only work the developer does is configuring the beans in the spring xml file.

Dependency injection-

Consider a class Employee

class Employee { 
   private int id;
   private String name;
   private Address address;

   Employee() {
     id = 10;
     name="name";
     address = new Address();
   }


}

and consider class Address

class Address {
   private String street;
   private String city;

   Address() {
     street="test";
     city="test1";

  }
}

In the above code the address class values will be set only when the Employee class is instantiated, which is dependency of Address class on Employee class. And spring solves this problem using Dependency Injection concept by providing two ways to inject this dependency.

  1. Setter injection

Setter method in Employee class which takes a reference of Address class

public void setAddress(Address addr) {
    this.address = addr;
}
  1. Constructor injection

Constructor in Employee class which accepts Address

Employee(Address addr) {
      this.address = addr;
}

In this way the Address class values can be set independently using either setter/constructor injection.


  • Spring helps in the creation of loosely coupled applications because of Dependency Injection.
  • In Spring, objects define their associations (dependencies) and do not worry about how they will get those dependencies. It is the responsibility of Spring to provide the required dependencies for creating objects.

For example: Suppose we have an object Employee and it has a dependency on object Address. We would define a bean corresponding to Employee that will define its dependency on object Address.

When Spring tries to create an Employee object, it will see that Employee has a dependency on Address, so it will first create the Address object (dependent object) and then inject it into the Employee object.

  • Inversion of Control (IoC) and Dependency Injection (DI) are used interchangeably. IoC is achieved through DI. DI is the process of providing the dependencies and IoC is the end result of DI. (Note: DI is not the only way to achieve IoC. There are other ways as well.)

  • By DI, the responsibility of creating objects is shifted from our application code to the Spring container; this phenomenon is called IoC.

  • Dependency Injection can be done by setter injection or constructor injection.

I shall write down my simple understanding of this two terms: (For quick understanding just read examples)

  • Dependency Injection(DI):
    Dependency injection generally means passing a dependent object as a parameter to a method, rather than having the method create the dependent object.
    What it means in practice is that the method does not have a direct dependency on a particular implementation; any implementation that meets the requirements can be passed as a parameter.

    With this implementation of objects defines their dependencies. And spring makes it available.
    This leads to loosely coupled application development.

    Quick Example:EMPLOYEE OBJECT WHEN CREATED,IT WILL AUTOMATICALLY CREATE ADDRESS OBJECT (if address is defines as dependency by Employee object)*.

  • Inversion of Control(IoC) Container:
    This is common characteristic of frameworks, IoC manages java objects
    - from instantiation to destruction through its BeanFactory.
    - Java components that are instantiated by the IoC container are called beans, and the IoC container manages a bean's scope, lifecycle events, and any AOP features for which it has been configured and coded.

    QUICK EXAMPLE:Inversion of Control is about getting freedom, more flexibility, and less dependency. When you are using a desktop computer, you are slaved (or say, controlled). You have to sit before a screen and look at it. Using keyboard to type and using mouse to navigate. And a bad written software can slave you even more. If you replaced your desktop with a laptop, then you somewhat inverted control. You can easily take it and move around. So now you can control where you are with your computer, instead of computer controlling it.

    By implementing Inversion of Control, a software/object consumer get more controls/options over the software/objects, instead of being controlled or having less options.

    Inversion of control as a design guideline serves the following purposes:
    - There is a decoupling of the execution of a certain task from implementation.
    - Every module can focus on what it is designed for.
    - Modules make no assumptions about what other systems do but rely on their contracts.
    - Replacing modules has no side effect on other modules

I will keep things abstract here, you can visit following links for detail understanding of the topic.

A good read with example

Detailed explanation


In Spring Objects are loosely coupled i.e., each class is independent of each other so that everything can be tested individually. But when using those classes, a class may be dependent on other classes which need to be instantiated first.

So, we tell spring that class A is dependent on class B. So, when creating bean(like class) for class A, it instantiates class B prior to that of class A and injects that in class A using setter or constructor DI methods. I.e., we are telling spring the dependency at run-time. This is DI.

As, we are assigning the responsibility of creating objects(beans), maintaining them and their aggregations to Spring instead of hard-coding it, we call it Inversion Of Control(IOC).