Dependency Injection · interview question
Why is field injection a design problem?
Multiple choiceMediumchapter-9-curatedpremiumdependency-injectionconstructor-vs-field-injection
Answer and explanation
Correct answer
B. It hides dependencies and makes the class harder to test directly
Why
Field injection makes dependencies less visible and usually pushes tests toward framework-heavy setup. Constructor injection exposes the collaboration graph and makes the class easier to instantiate safely.
Test yourself on Dependency Injection
A free test with instant, explained results. No account needed.
More Dependency Injection questions
- In Spring Boot, how can you ensure that a bean is eagerly initialized at startup?
- What advantage does constructor injection offer over field injection concerning encapsulation?
- In Spring Boot, how does lazy initialization of beans affect constructor injection?
- How does Spring Boot handle dependency injection when multiple beans match the type being injected?
- Which scope of a bean in Spring Boot is the default in Java?
- Why do teams use the @Autowired annotation in Spring Boot in production code?