Inversion of Control and Dependency Injection in Spring Framework

Nirmal kumar
3 min readSep 26, 2021

Inversion of Control

Inversion of control is a programming principle that means giving up control of one object to some other object that knows how to deal with it. This principle is used in some frameworks, such as Spring.

It is a design pattern that breaks the dependencies between objects and instead depends on abstractions. Inversion of Control can be achieved through Dependency Injection, a technique for separating software components from concerns or responsibilities.

Dependency Injection

Dependency injection is a sub-type of Inversion of Control and is implemented by constructor injection, setter injection, or method injection and it deals with how components get hold of their dependencies.

It is a process where objects define their dependencies through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern. hence the name Inversion of Control.

Inversion of Control is a principle by which the control of objects is transferred to a container or framework. Dependency injection is a pattern through which IoC is implemented and the act of connecting objects with other objects or injecting objects into objects is done by the container rather than by the object themselves.

In Spring framework there are two types of Dependency injection:-

  • Constructor Injection: This is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency i.e, all the dependencies are declared in one step. This helps in understanding whether the class depends on too many services. Constructor injection is the best practice to inject dependencies.
  • Setter Injection: This is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean. The container will call the setter methods of our class. The objects of the class will have setters. IoC container will use these setters to provide the resource at run-time. It is used for optional dependencies.

Why we need Dependency Injection?

Dependency Injection provides objects that an object needs. So rather than the dependencies construct themselves they are injected by some external means. For instance, we have a class called “Customer” who uses a “Logger” class to log errors. So rather than creating the “Logger” from within the class, we can inject the same via a constructor as shown below.

The biggest benefit achieved by the this approach is “Decoupling”. We can invoke the customer object and pass any kind of “Logger” object.

Advantages of Inversion of Control and Dependency Injection

  1. Dependency injection saves developers from the need to find and create all the objects themselves. It also reduces the risk of forgetting to wire up some important object.
  2. A basic benefit of dependency injection is decreased coupling between classes and their dependencies.
  3. The most important benefit of dependency injection is maintainability. If your classes are loosely coupled then it makes your code easier to maintain.
  4. DI makes the code readable as code is straightforward to understand.

Conclusion

With dependency injection, we can easily swap out different implementations for certain dependencies without changing any code. We can also use dependency injection with a testing framework like JUnit or Mockito which can help us test complex code more thoroughly and predictably.

Refrences

  1. Spring.io
  2. Perfomatix

--

--