Constructor Injection vs Field Injection vs Setter Injection
Fast recall
Quick revision
Core answer
new — this is the Spring team's recommended approach. Field injection uses @Autowired on fields directly (concise but not testable without a container, no final possible). Setter injection uses @Autowired on setter methods, allowing optional or re-injectable dependencies. The decisive differences: only constructor injection allows final fields, surfaces circular dependencies at startup with BeanCurrentlyInCreationException, and works in plain JUnit tests without Spring. Lombok's @RequiredArgsConstructor eliminates the boilerplate that used to justify field injection.Understanding of testability trade-offs and the reasoning behind the Spring team's preference for constructor injection.
Thinking all three are equivalent for production use; not knowing that field injection hides circular dependencies and makes unit testing harder.
Mention that constructor injection exposes circular dependencies at startup (`BeanCurrentlyInCreationException`) rather than hiding them, and that it enables immutable design with `final` fields.
Interview-ready guide
Interview answer
Spring supports three injection styles, but they are not equally valid alternatives. Constructor injection is the Spring team's recommended approach for all mandatory dependencies.
Constructor injection
- Dependencies passed as constructor parameters — visible from the signature, available from line one of the constructor body
- Fields can be
final, making the class immutable after construction; safe publication guaranteed by the JVM - Testable with plain
new OrderService(mockRepo, mockPayment)— no Spring context, no reflection, tests in milliseconds - Fails fast on circular dependencies:
BeanCurrentlyInCreationExceptionat startup, naming the beans involved - Lombok's
@RequiredArgsConstructoreliminates the boilerplate; Kotlin primary constructors work without any annotations
Field injection
@Autowireddirectly on private fields — looks clean but the simplicity is deceptive- Dependencies are invisible from outside the class; a class with 12 field-injected dependencies looks tidy while violating Single Responsibility
- Fields cannot be
finalbecause reflection writes after construction - Unit testing requires
ReflectionTestUtils.setFieldor a full@SpringBootTestcontext — an order of magnitude slower - Spring Boot 2.6+ blocks circular references for all injection styles by default
Setter injection
@Autowiredon setter methods — dependencies visible via method signatures, settable in tests, but cannot befinal- Legitimate use case: genuinely optional dependencies that enhance behavior without being required
- Callers must null-check since the setter may never be called
The rule: constructor injection for all mandatory dependencies (99% of cases), setter injection only for truly optional ones, never field injection in production code. When a constructor grows past 5–7 parameters, the class has too many responsibilities and needs refactoring.
Constructor Injection vs Field Injection vs Setter Injection
Direct Answer
Constructor injection wires dependencies through the constructor, allowing final fields and making the class testable with plain new — this is the Spring team's recommended approach. Field injection uses @Autowired on fields directly (concise but not testable without a container, no final possible). Setter injection uses @Autowired on setter methods, allowing optional or re-injectable dependencies. The decisive differences: only constructor injection allows final fields, surfaces circular dependencies at startup with BeanCurrentlyInCreationException, and works in plain JUnit tests without Spring. Lombok's @RequiredArgsConstructor eliminates the boilerplate that used to justify field injection.
Common Mistakes & Pitfalls
Thinking all three are equivalent for production use; not knowing that field injection hides circular dependencies and makes unit testing harder.
Check your understanding
Answer these without looking back at the article.
- How does Spring handle circular dependencies differently for constructor injection vs field injection?
- What does 'immutable bean design' mean and why does constructor injection enable it?
- When is setter injection the right choice over constructor injection?
- How does Lombok's @RequiredArgsConstructor interact with Spring's single-constructor auto-detection?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.