What are the Types of Dependency Injection in Spring?
Dependency Injection (DI) is one of the core concepts of the Spring Framework. It allows the Spring IoC Container to automatically provide required dependencies to objects instead of developers creating them manually.
Spring Framework mainly supports three types of Dependency Injection:
- Constructor Injection
- Setter Injection
- Field Injection
These dependency injection types help build:
- Loosely coupled applications
- Scalable systems
- Maintainable enterprise applications
- Microservices architectures
- Cloud-native applications
What is Dependency Injection?
Dependency Injection is a design pattern where Spring automatically injects required dependencies into application classes.
Instead of:
NotificationService service =
new NotificationService();
Spring creates and injects objects automatically.
Real-Time Banking Example
Consider a banking application with:
- Payment Service
- Notification Service
- Fraud Detection Service
- Audit Service
PaymentService depends on NotificationService.
Spring automatically injects NotificationService into PaymentService using Dependency Injection.
Main Types of Dependency Injection
| Injection Type | Description |
|---|---|
| Constructor Injection | Dependencies injected through constructor |
| Setter Injection | Dependencies injected through setter methods |
| Field Injection | Dependencies injected directly into fields |
1. Constructor Injection
Constructor Injection is a type of Dependency Injection where dependencies are provided through the constructor of the class.
This is the most recommended and widely used Dependency Injection type in enterprise applications.
Constructor Injection Example
@Service
public class PaymentService {
private final NotificationService notificationService;
@Autowired
public PaymentService(
NotificationService notificationService) {
this.notificationService =
notificationService;
}
}
Here:
- Spring creates NotificationService
- Spring injects it through constructor
- PaymentService becomes loosely coupled
How Constructor Injection Works
Spring Container Starts
|
Creates NotificationService Bean
|
Calls PaymentService Constructor
|
Injects NotificationService
Advantages of Constructor Injection
- Recommended for enterprise applications
- Supports immutable dependencies
- Better unit testing
- Mandatory dependency validation
- Cleaner architecture
Why Constructor Injection is Preferred
Constructor Injection ensures that:
- Required dependencies are always available
- Objects remain immutable
- Applications become more reliable
Real-Time Banking Example
PaymentService
|
Constructor Injection
|
NotificationService
PaymentService cannot work without NotificationService.
Constructor Injection guarantees dependency availability.
2. Setter Injection
Setter Injection is a type of Dependency Injection where dependencies are injected using setter methods.
Setter Injection Example
@Service
public class PaymentService {
private NotificationService notificationService;
@Autowired
public void setNotificationService(
NotificationService notificationService) {
this.notificationService =
notificationService;
}
}
Here:
- Spring creates NotificationService
- Spring calls setter method
- Dependency gets injected dynamically
How Setter Injection Works
Spring Container Starts
|
Creates PaymentService Bean
|
Creates NotificationService Bean
|
Calls Setter Method
|
Injects Dependency
Advantages of Setter Injection
- Supports optional dependencies
- Flexible dependency changes
- Easy reconfiguration
Disadvantages of Setter Injection
- Object may exist without dependencies
- Dependencies are mutable
- Less secure than constructor injection
When to Use Setter Injection
Setter Injection is useful when:
- Dependencies are optional
- Dependencies may change dynamically
- Configuration flexibility is required
3. Field Injection
Field Injection is a type of Dependency Injection where dependencies are injected directly into class fields.
Field Injection Example
@Service
public class PaymentService {
@Autowired
private NotificationService notificationService;
}
Spring directly injects dependency into the field.
How Field Injection Works
Spring Container Starts
|
Creates Bean
|
Uses Reflection
|
Injects Dependency into Field
Advantages of Field Injection
- Simple syntax
- Less boilerplate code
- Easy for beginners
Disadvantages of Field Injection
- Difficult unit testing
- Hidden dependencies
- Less maintainable
- Cannot create immutable objects
Therefore, field injection is generally not recommended for large enterprise applications.
Comparison of Injection Types
| Feature | Constructor Injection | Setter Injection | Field Injection |
|---|---|---|---|
| Recommended | Highly Recommended | Sometimes | Less Recommended |
| Immutable Dependencies | Yes | No | No |
| Supports Optional Dependencies | No | Yes | Yes |
| Unit Testing | Easy | Moderate | Difficult |
| Maintainability | Excellent | Good | Poor |
Dependency Injection Using Interfaces
Dependency Injection becomes more powerful with interfaces.
Example
public interface NotificationService {
void sendNotification();
}
@Service
public class EmailService
implements NotificationService {
}
@Service
public class SMSService
implements NotificationService {
}
Spring can inject different implementations dynamically.
Dependency Injection in Spring Boot
Spring Boot heavily uses Dependency Injection for:
- Auto-configuration
- REST APIs
- Microservices
- Database integration
- Security configuration
Dependency Injection in Microservices
In Microservices Architecture:
- Services remain loosely coupled
- Dependencies are managed efficiently
- Testing becomes easier
- Applications scale independently
Microservices Banking Example
Payment Service
|
Notification Service
|
Fraud Detection Service
Spring automatically injects dependencies between services and components.
Common Spring Annotations Used
- @Autowired
- @Component
- @Service
- @Repository
- @Controller
- @Bean
Best Practice Recommendation
Most enterprise applications prefer:
Constructor Injection
Because it provides:
- Better reliability
- Better testing support
- Immutable objects
- Cleaner architecture
Common Interview Questions
What are the types of Dependency Injection in Spring?
Spring supports Constructor Injection, Setter Injection, and Field Injection.
Which Dependency Injection type is best?
Constructor Injection is considered the best and most recommended approach in enterprise applications.
Why is Field Injection less recommended?
Field Injection creates hidden dependencies and makes unit testing difficult.
Professional Interview Answer
Spring Framework mainly supports three types of Dependency Injection: Constructor Injection, Setter Injection, and Field Injection. Constructor Injection injects dependencies through constructors and is the most recommended approach because it supports immutability and easier testing. Setter Injection injects dependencies through setter methods and is useful for optional dependencies. Field Injection injects dependencies directly into fields using reflection but is generally less recommended for enterprise applications due to maintainability and testing challenges.
Conclusion
Dependency Injection is one of the most important features of Spring Framework and Spring Boot.
Spring provides Constructor Injection, Setter Injection, and Field Injection to manage dependencies efficiently in enterprise applications.
Constructor Injection is considered the best practice because it improves maintainability, testing, scalability, and reliability in modern enterprise systems and microservices architectures.
Understanding the types of Dependency Injection is essential for Java developers, Spring Boot developers, backend engineers, and software architects preparing for interviews and building enterprise-grade applications.