Bird
0
0

Why does Spring Boot recommend avoiding field injection in favor of constructor injection from a design perspective?

hard📝 Conceptual Q10 of 15
Spring Boot - Inversion of Control and Dependency Injection
Why does Spring Boot recommend avoiding field injection in favor of constructor injection from a design perspective?
AField injection automatically validates dependencies at compile time
BField injection reduces boilerplate code and improves readability
CConstructor injection allows dependencies to be changed at runtime
DConstructor injection enforces immutability and clear dependency contracts
Step-by-Step Solution
Solution:
  1. Step 1: Understand design benefits of constructor injection

    Constructor injection requires dependencies to be provided at creation, making them final and immutable.
  2. Step 2: Contrast with field injection

    Field injection hides dependencies and allows mutable state, reducing design clarity and safety.
  3. Final Answer:

    Constructor injection enforces immutability and clear dependency contracts -> Option D
  4. Quick Check:

    Design reason to avoid field injection = D [OK]
Quick Trick: Constructor injection enforces clear, immutable dependencies [OK]
Common Mistakes:
  • Thinking field injection improves readability
  • Believing constructor injection allows runtime changes
  • Assuming field injection validates dependencies at compile time

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes