Skip to content
Sharp

Spring Core · interview question

In a Spring Boot e-commerce application, why is constructor injection preferred over field injection for a @Service class?

Multiple choiceMediumchapter-9-curatedpremiumspring-corecontrollers-services-repositories
Choose one answer
Answer and explanation

Correct answer

B. It allows for easier testing and immutability.

Why

Constructor injection is preferred because it promotes immutability and makes the class easier to test by explicitly requiring its dependencies. With constructor injection, the dependencies are required at instantiation, which helps catch errors early during application startup. Field injection, on the other hand, can result in hidden dependencies and make testing more challenging.

Test yourself on Spring Core

A free test with instant, explained results. No account needed.

Start free test
All Spring Core questions →