🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Dependency Injection (DI) in Spring


DI Introduction

  • Dependency Injection is a "Design Pattern" that is used to implement IoC(Inversion of Control) principal.
  • Dependency Injection (DI) is a technique where one object (known as the dependency) is "injected" into another object (the dependent object).
  • Below is the simple diagram showing dependency injection in which Address object is "injected" int Student object:

    Dependency Injection in Spring by Deepak Sir Smart programming

    Here Student (dependent object) depends on Address (injected dependency), thus injecting Address in Student is known as "Dependency Injection".

  • In Spring, Dependency Injection is commonly called "wiring" because it involves connecting or "wiring" components with their required dependencies.

Purpose of DI

  • The primary goal of DI is to decouple classes from each other, making them easier to manage, test, and reuse.
  • Decoupling classes means reducing or removing direct dependencies between them. In other words, classes don’t directly depend on specific implementations of other classes but rely on abstractions or interfaces instead. This separation allows each class to function independently, making the code more flexible, modular, and easier to maintain.
    When classes are decoupled, changes in one class (such as updating or replacing it) are less likely to impact other classes, which helps in testing, managing and reusing code across different parts of the application.

DI Program

  • Click Here for Dependency Injection Program (Comparing Between Core Java & Spring)

Advantages of DI

  1. Loose Coupling: Classes rely on interfaces or abstract definitions instead of specific implementations, making it easy to swap out parts without affecting other parts.
  2. Improved Testability: Dependencies can be replaced with mock versions, which makes it easier to isolate and test each class by itself.
  3. Flexibility and Maintainability: Since dependencies are provided from the outside, we can change them without altering the actual class, making it easy to update or reconfigure behavior.
  4. Easier Configuration Management: Spring’s configuration files or annotations allow you to define and manage all dependencies in one place, simplifying setup and changes.
  5. Lifecycle Management: Spring takes care of creating, initializing, and destroying objects (beans) as needed, so you don’t have to manage these stages manually.

DI Mechanisms

DI Mechanisms means different ways by which dependencies are injected into beans. Spring provides three DI mechanisms which are as follows:
  1. Constructor Injection:
    • In Constructor Injection, dependencies are provided through the class constructor.
    • It makes sure that all the necessary parts (dependencies) are provided as soon as the object is created, so it’s ready to use.
    • It is best suited when dependencies are mandatory and should not be modified after object creation.
    • Click Here to read Constructor Injection more deeply.
  2. Setter Injection:
    • In Setter Injection, dependencies are injected through public setter methods after the object is created.
    • It allows optional dependencies and supports mutable dependencies that can be changed after creation.
    • Click Here to read Setter Injection more deeply.
  3. Field Injection (Not Recommended in Spring, but supported with @Autowired):
    • In Field Injection, dependencies are injected directly into fields.
    • It is commonly used in frameworks that support reflection-based injection, but makes testing and managing dependencies harder in Spring.
    • Spring promotes constructor or setter injection as compared to field injection for better testability and clarity.
DI Mechanism sometimes known as DI techniques or DI Methods.