Constructor injection is the preferred dependency injection approach in Spring because it ensures that all required dependencies are provided when an object is created. This results in more reliable, immutable, and testable classes while preventing partially initialized objects.
Consider an OrderService that requires a PaymentService to function. With constructor injection, the OrderService cannot be created unless a valid PaymentService is provided, ensuring the object is always in a valid state.
@Service
public class OrderService {
private final PaymentService paymentService;
public OrderService(
PaymentService paymentService) {
this.paymentService =
paymentService;
}
public void placeOrder() {
paymentService.processPayment();
}
}@Service
public class OrderService {
private PaymentService paymentService;
@Autowired
public void setPaymentService(
PaymentService paymentService) {
this.paymentService =
paymentService;
}
}A concise interview answer is: Constructor injection is recommended because it guarantees that all required dependencies are available at object creation time, prevents partially initialized objects, supports immutability through final fields, and improves testability. Setter injection is generally reserved for optional or configurable dependencies.
Last updated: 11 November 2026
Previous question: What type of injection does @Autowired use?
Next question: What is AOP, and what is its biggest disadvantage?