What is Dependency Injection?

What is Dependency Injection?

Dependency Injection (DI) is a design pattern in which an object's dependency is injected by the framework rather than by the object itself. It reduces coupling between multiple objects as it is dynamically injected by the framework. In DI, the framework is completely responsible for reading configuration.

Advantages of Dependency Injection

The advantages of DI are as follows:

  • Loosely coupled architecture.
  • Separation of responsibility.
  • Configuration and code are separate.
  • A different implementation can be supplied using configuration without changing the code dependent.
  • Improves testability.
  • DI allows you to replace actual objects with mock objects. This improves testability by writing simple JUnit tests that use mock objects.