What is Dependency Injection in Spring?
Dependency Injection (DI) is one of the core concepts of the Spring Framework.
It is a design pattern where objects are created and injected by the Spring Container instead of being created manually by developers.
In simple words, Spring automatically provides required objects to another object whenever needed.
Simple Definition
Dependency Injection is a technique where Spring automatically injects dependent objects into a class.
Dependency Injection = Giving required objects to another object automatically.
What is a Dependency?
A dependency means one object depends on another object to perform its work.
Example:
public class PaymentService {
private RazorpayService razorpayService;
}
Here:
PaymentServicedepends onRazorpayServiceRazorpayServiceis called a dependency
Problem Without Dependency Injection
Without DI, developers manually create objects:
public class PaymentService {
private RazorpayService razorpayService =
new RazorpayService();
}
Problems:
- Tight coupling
- Difficult testing
- Hard to maintain
- Less flexibility
- Object creation logic everywhere
How Dependency Injection Solves This
Spring automatically creates objects and injects them.
@Service
public class PaymentService {
@Autowired
private RazorpayService razorpayService;
}
Spring Container:
- Creates RazorpayService object
- Creates PaymentService object
- Injects RazorpayService into PaymentService
Real-Time Example
Suppose we are developing Dhanish Empower.
The platform may contain:
- User Service
- Course Service
- Payment Service
- Email Service
PaymentService may need:
- RazorpayService
- EmailService
- UserRepository
Instead of manually creating all objects, Spring injects them automatically.
Types of Dependency Injection
| Type | Description |
|---|---|
| Constructor Injection | Dependencies are injected through constructor. |
| Setter Injection | Dependencies are injected using setter methods. |
| Field Injection | Dependencies are injected directly into fields. |
1. Constructor Injection
Dependencies are provided through constructor parameters.
@Service
public class PaymentService {
private final RazorpayService razorpayService;
public PaymentService(
RazorpayService razorpayService) {
this.razorpayService = razorpayService;
}
}
Advantages
- Recommended by Spring
- Supports immutability
- Easy testing
- Better maintainability
2. Setter Injection
Dependencies are injected using setter methods.
@Service
public class PaymentService {
private RazorpayService razorpayService;
@Autowired
public void setRazorpayService(
RazorpayService razorpayService) {
this.razorpayService = razorpayService;
}
}
3. Field Injection
Dependencies are injected directly into fields.
@Service
public class PaymentService {
@Autowired
private RazorpayService razorpayService;
}
Disadvantages
- Harder to test
- Less recommended for production
- Not immutable
Which Injection Type is Best?
Constructor Injection is considered best practice.
| Injection Type | Recommended? |
|---|---|
| Constructor Injection | Yes ✅ |
| Setter Injection | Sometimes |
| Field Injection | No ❌ |
Dependency Injection and IoC
Dependency Injection works together with Inversion of Control (IoC).
Normally:
- Developers create objects manually.
With IoC:
- Spring Container controls object creation.
Dependency Injection is the process used by IoC Container to inject required objects.
How Spring Container Performs Dependency Injection
- Spring scans components using Component Scan.
- Beans are created in the IoC Container.
- Dependencies are identified.
- Spring injects required beans automatically.
- Application becomes ready.
Important Annotations
| Annotation | Purpose |
|---|---|
@Autowired |
Automatically injects dependencies. |
@Component |
Marks class as Spring Bean. |
@Service |
Marks business logic class. |
@Repository |
Marks database layer class. |
@Controller |
Marks controller class. |
Advantages of Dependency Injection
- Reduces tight coupling
- Improves code maintainability
- Easy unit testing
- Improves flexibility
- Promotes reusable code
- Centralized object management
- Cleaner architecture
- Supports scalable applications
Production-Level Importance
Dependency Injection is highly important in enterprise applications.
It helps manage:
- Microservices
- Database services
- Payment gateways
- Notification services
- Security modules
- Cloud integrations
Large applications become easier to maintain and extend.
Interview Answer
Dependency Injection is a design pattern where the Spring Container automatically creates and injects dependent objects into a class.
It reduces tight coupling, improves maintainability, and simplifies testing.
Spring supports Constructor Injection, Setter Injection, and Field Injection.
Short Interview Answer
Dependency Injection is a process where Spring automatically injects required objects into another object.
Frequently Asked Questions
What is the purpose of Dependency Injection?
To reduce tight coupling and improve maintainability.
Which Dependency Injection type is best?
Constructor Injection is considered best practice.
What is @Autowired?
It is an annotation used to inject dependencies automatically.
What is IoC?
Inversion of Control means Spring Container controls object creation.
Can Spring perform Dependency Injection automatically?
Yes, Spring automatically injects dependencies using the IoC Container.
Conclusion
Dependency Injection is one of the most important concepts in Spring Framework and Spring Boot.
It simplifies object management, reduces coupling, improves maintainability, and helps build scalable enterprise applications.