Why is constructor injection recommended over setter injection?

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.

Key Points

Example

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();
    }
}

Setter Injection Example

@Service
public class OrderService {

    private PaymentService paymentService;

    @Autowired
    public void setPaymentService(
            PaymentService paymentService) {

        this.paymentService =
                paymentService;
    }
}

Issue

Advantages of Constructor Injection

When to Use Setter Injection

Interview Tip

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?

More Spring Framework - Level II interview questions

All Spring Framework - Level II questions