Why Spring Field Injection Is Discouraged and What to Use Instead
This article explains why Spring’s field injection is discouraged, compares constructor‑, setter‑, and field‑based injection methods, outlines the drawbacks of field injection such as final‑field incompatibility and hidden dependencies, and recommends using constructor injection for required beans and setter injection for optional ones.
Introduction
When editing code in IntelliJ IDEA, a warning appears on a field injection example. The warning states that field injection is not recommended and that Spring recommends constructor‑based dependency injection with assertions for mandatory dependencies.
@Autowire
private JdbcTemplate jdbcTemplate;Field injection is not recommended. Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
Injection Types
Although the Spring Framework documentation defines two main injection types, there are actually 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 is the most widely used because of its simplicity, but it triggers a warning in IDEs and is discouraged by the Spring team.
Drawbacks of Field Injection
Cannot be used with final fields; constructor injection is required for immutable dependencies.
Obscures the Single Responsibility Principle; a class with many injected fields may indicate a violation.
Tightly couples the class to Spring’s IoC container, making it hard to re‑configure outside the container.
Hides dependencies by keeping them private, reducing the class’s explicit contract.
Injected beans are not validated at startup, so null errors surface only during business execution.
Cannot perform custom checks or logic during injection, making configuration errors harder to detect early.
Conclusion
Field injection has many disadvantages. It is advisable to avoid it and prefer constructor injection for mandatory dependencies—making them immutable and non‑null—and setter injection for optional dependencies.
Postscript
Translated from “field‑injection‑is‑not‑recommended” and enriched with personal interpretation.
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.
