Learning Objectives
- Trace how Spring’s `ApplicationContext` creates, resolves, and caches Singleton beans at startup.
- Explain why constructor injection beats field `@Autowired` for immutability and unit testing.
- Handle multiple interface implementations safely with `@Primary` or `@Qualifier`.
Prerequisites
- Core Java Classes, Interfaces, and Polymorphism
What Problem Does Dependency Injection Solve?
When a controller runs new RazorpayGateway() directly inside its constructor, swapping payment providers or writing fast isolated unit tests without network calls becomes painful. Spring Inversion of Control (IoC) flips ownership: components declare required interfaces in their constructor, and Spring’s container instantiates and injects the matching bean at startup.
Since Spring Framework 4.3+, single-constructor classes do not even require @Autowired — constructor parameters automatically become managed dependencies.
Continue learning with Fixing Spring Data JPA N+1 Queries or practise Spring Boot Mock Tests inside our Java Backend Career Program.
Practical Programming Examples
Constructor-Injected Multi-Implementation Service Pattern (Spring Boot 3)
Wire a domain NotificationSender interface cleanly into OrderService without brittle field @Autowired annotations.
public interface NotificationSender {
void sendReceipt(String recipient, long totalInr);
}
@Service
public class EmailNotificationSender implements NotificationSender {
@Override
public void sendReceipt(String recipient, long totalInr) {
System.out.println("Sent GST receipt for INR " + totalInr + " to " + recipient);
}
}
@Service
public class EnrollmentService {
private final NotificationSender notifier;
public EnrollmentService(NotificationSender notifier) {
this.notifier = notifier;
}
public void confirmSeat(String email) {
notifier.sendReceipt(email, 53_100L);
}
}
Sent GST receipt for INR 53100 to student@example.com
- Spring component scanning registers `EmailNotificationSender` as the bean satisfying `NotificationSender`.
- When instantiating `EnrollmentService`, the IoC container invokes its single constructor and passes the managed bean.
- Declaring `private final` ensures `notifier` can never be null or reassigned mid-request.
Common Mistakes to Avoid
- Injecting concrete implementation classes instead of interfaces where interchangeable strategies belong.
- Using `@Autowired` on private fields, hiding mandatory dependencies until runtime.
Production Best Practices
- Default to constructor injection + `private final` dependencies across every `@Service` and `@RestController`.
- Break circular A ↔ B dependencies cleanly by extracting a shared C coordinator or domain event listener.
Technical Interview Relevance
Top Spring Boot interview topic covering ApplicationContext vs BeanFactory, Singleton vs Prototype scopes, and proxy self-invocation traps.
Practice & Self-Check Questions
Are Spring Singleton beans thread-safe automatically?
No — Singleton simply means one instance per container. Keep service beans stateless (store per-request state in local variables, not instance fields).