Why Field Injection in Spring Is Discouraged and What to Use Instead
This article explains Spring's warning about field injection, compares constructor, setter, and field injection methods, outlines the drawbacks of field injection, and recommends using constructor or setter injection for safer, more maintainable Java applications.
Introduction
When writing code in IntelliJ IDEA, a warning appears on a field annotated with @Autowire for a JdbcTemplate instance.
Field injection is not recommended. Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
The Spring team advises against field injection and suggests using constructor‑based injection.
Injection Types
Spring Framework (5.0.3) officially defines two injection types, but in practice there are three:
Constructor‑based injection
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Autowire
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
}Setter‑based injection
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Autowire
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}Field‑based injection
public class UserServiceImpl implements UserService {
@Autowire
private UserDao userDao;
}Field injection triggers a warning in IDEA, yet it remains popular due to its simplicity.
Drawbacks of Field Injection
Cannot be used with final fields : Spring's IoC sets properties via setters, which cannot initialize final variables; constructor injection is required.
Obscures single‑responsibility principle : Excessive constructor parameters may indicate a violation of SRP, while field injection hides this issue.
Tightly couples code to Spring's IoC : Classes become dependent on the container and lose usefulness outside the Spring context.
Hides dependencies : Private fields conceal required collaborators, making the class's contract less clear.
Cannot validate injected properties at startup : Null‑related errors surface only during business execution, delaying detection.
Lacks control over injection timing : Custom validation or configuration checks cannot be performed during injection.
Conclusion
Field injection has many disadvantages; it should be avoided. Prefer constructor‑based injection for mandatory dependencies to make them immutable and non‑null, and use setter‑based injection for optional dependencies.
Postscript
Translated from "field‑injection‑is‑not‑recommended" with added personal explanations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
