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:
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
Loose Coupling: Classes rely on interfaces or abstract definitions instead of specific implementations, making it easy to swap out parts without affecting other parts.
Improved Testability: Dependencies can be replaced with mock versions, which makes it easier to isolate and test each class by itself.
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.
Easier Configuration Management: Spring’s configuration files or annotations allow you to define and manage all dependencies in one place, simplifying setup and changes.
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:
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.
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.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.