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.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Spring Field Injection Is Discouraged and What to Use Instead

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.

Field injection warning
Field injection warning

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendConstructor InjectionField InjectionSetter Injection
Java Backend Technology
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.