Why Field Injection Is Discouraged in Spring and Preferred Alternatives
The article explains that Spring no longer recommends @Autowired field injection, describes constructor‑based, setter‑based, and field‑based dependency injection types, illustrates each with code examples, and outlines the drawbacks of field injection such as immutability issues, SRP violations, and tight coupling to the container.
Spring officially discourages the use of @Autowired field/property injection, and many companies have banned it in new projects.
After upgrading from Spring 3.0 to 5.0, the author noticed IDE warnings on @Autowired fields, prompting a review of dependency injection (DI) types supported by Spring.
Types of Dependency Injection
Spring defines three main DI approaches: constructor‑based, setter‑based, and field‑based injection. While field injection is common, IDEs now flag it as a bad practice.
2.1 Constructor‑Based Injection
In constructor injection, the class constructor is annotated with @Autowired and receives all required dependencies as parameters, allowing the injected fields to be declared final for immutability.
@Component
public class ConstructorBasedInjection {
private final InjectedBean injectedBean;
@Autowired
public ConstructorBasedInjection(InjectedBean injectedBean) {
this.injectedBean = injectedBean;
}
}The @Autowired annotation on the constructor can be omitted according to Spring documentation.
public class SimpleMovieLister {
// the SimpleMovieLister has a dependency on a MovieFinder
private MovieFinder movieFinder;
// a constructor so that the Spring container can inject a MovieFinder
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// business logic omitted...
}Constructor injection’s main advantage is that required dependencies can be declared final , ensuring they are initialized during object creation.
2.2 Setter‑Based Injection
Setter injection marks a setter method with @Autowired. When a bean is instantiated via a no‑arg constructor or static factory method, Spring calls the setter to inject the dependency.
@Component
public class SetterBasedInjection {
private InjectedBean injectedBean;
@Autowired
public void setInjectedBean(InjectedBean injectedBean) {
this.injectedBean = injectedBean;
}
}Like constructor injection, the @Autowired annotation on the setter can be omitted.
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// business logic omitted...
}2.3 Field‑Based Injection
Field injection annotates a class field with @Autowired, and Spring sets the field after the object is instantiated.
@Component
public class FieldBasedInjection {
@Autowired
private InjectedBean injectedBean;
}Although concise, field injection has several drawbacks.
Drawbacks of Field Injection
3.1 Cannot Declare Immutable Fields
Field injection cannot be used with final fields, preventing immutable dependencies; only constructor injection can achieve this.
3.2 Violates Single‑Responsibility Principle
Using field injection can hide the growing number of dependencies inside a class, making it harder to notice when a class becomes overly complex, whereas a large constructor immediately signals too many required dependencies.
3.3 Tight Coupling to the DI Container
Field injection ties the class to the Spring container, making it difficult to instantiate the class outside the container (e.g., in unit tests) without using reflection.
3.4 Hides Dependencies
Field injection obscures a class’s required collaborators, whereas constructor or setter injection makes dependencies explicit through the class’s public API.
Summary
Field injection should be avoided due to immutability issues, SRP violations, and tight container coupling. Prefer constructor injection for mandatory dependencies (declare them final ) and setter injection for optional ones.
References
Field injection is not recommended – Spring IOC by Marc Nuri
Spring official documentation – 1.4. Dependencies
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.