All posts / Spring Boot

Why Constructor Injection Is Better Than Field Injection in Spring Boot

NordLearn · · 1 min read

On this page

Introduction

Dependency Injection (DI) is a core concept in Spring Boot.
While Spring supports multiple injection styles, constructor injection is considered the best practice, especially for production-ready applications.

In this article, you’ll learn:

  • Why constructor injection is preferred
  • Why field injection makes testing harder
  • JUnit test comparison
  • How field injection breaks encapsulation
  • Best practices recommended by Spring

Field Injection in Spring Boot

@Service
public class OrderService {

    @Autowired
    private PaymentService paymentService;

    public void placeOrder() {
        paymentService.processPayment();
    }
}

Problems with Field Injection

  • Hidden dependencies
  • Breaks encapsulation
  • Harder unit testing
  • Requires reflection or Spring context

Constructor Injection in Spring Boot

@Service
public class OrderService {

    private final PaymentService paymentService;

    public OrderService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void placeOrder() {
        paymentService.processPayment();
    }
}

Since Spring 4.3+, @Autowired is optional when a class has only one constructor. Link

Why This Is Better

  • Dependencies are explicit
  • Supports immutability
  • Easier unit testing
  • POJO-friendly

JUnit Testing Comparison

Shared Dependency

public interface PaymentService {
    void processPayment();
}

Field Injection — JUnit Test

@SpringBootTest
class OrderServiceFieldTest {

    @Autowired
    private OrderService orderService;

    @MockBean
    private PaymentService paymentService;

    @Test
    void shouldPlaceOrder() {
        orderService.placeOrder();
        verify(paymentService).processPayment();
    }
}

Problems:

  • Starts Spring container
  • Slow execution

Or uses reflection -> Reflection breaks encapsulation.

@InjectMocks
private OrderService orderService;

Constructor Injection — JUnit Test

@ExtendWith(MockitoExtension.class)
class OrderServiceConstructorTest {

    @Mock
    private PaymentService paymentService;

    private OrderService orderService;

    @BeforeEach
    void setUp() {
        orderService = new OrderService(paymentService);
    }

    @Test
    void shouldPlaceOrder() {
        orderService.placeOrder();
        verify(paymentService).processPayment();
    }
}

✅ Fast
✅ No Spring
✅ Clean
✅ True unit test

Why Field Injection Breaks Encapsulation

Field Injection Lifecycle

  1. Object is created with no arguments
  2. Dependencies are null
  3. Spring modifies private fields using reflection

This violates:

  • Object integrity
  • Encapsulation
  • Constructor invariants

Constructor injection ensures valid objects from the start.

Final Verdict

Constructor injection leads to better design, safer code, and faster tests.

For any serious Spring Boot application, constructor injection should be the default choice.