Spring supports three ways to inject a bean's dependencies — constructor, setter, and field injection — each wiring collaborating objects together differently at bean creation time.
A UserService that absolutely needs a UserRepository to function should use constructor injection so it's impossible to construct the service in an invalid, half-wired state.
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}A concise interview answer is
Spring supports constructor, setter, and field injection. Constructor injection is my default because it makes required dependencies explicit, allows final fields, and is easy to unit test without needing Spring at all — setter and field injection are more useful for optional or rarely-changed dependencies.
Last updated: 11 November 2026
Previous question: How does Spring MVC utilize dependency injection with controllers?
Next question: What are the benefits of using dependency injection in web applications?