Backend Development 9 min read

Why Spring Discourages Field Injection: A Comparison of Constructor, Setter, and Field Injection

The article explains why Spring discourages field injection, compares constructor, setter, and field injection types, provides Java code examples, and outlines the drawbacks of field injection such as immutability issues, SRP violations, container coupling, and hidden dependencies.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Why Spring Discourages Field Injection: A Comparison of Constructor, Setter, and Field Injection

Spring has deprecated field (property) injection with @Autowired in favor of constructor and setter injection, and many companies now forbid its use.

The framework upgrade from Spring 3.0 to 5.0 triggered IDE warnings about field injection, reflecting the change that began with Spring 4.0, which recommended constructor and setter injection instead.

Types of Dependency Injection

Spring defines three injection styles:

Constructor‑based injection

Setter‑based injection

Field‑based injection

Field injection is widely used but IDEs flag it as discouraged.

2.1 Constructor‑based Injection

In this style the class constructor is annotated with @Autowired and receives all required dependencies as parameters.

@Component
public class ConstructorBasedInjection {
    private final InjectedBean injectedBean;

    @Autowired
    public ConstructorBasedInjection(InjectedBean injectedBean) {
        this.injectedBean = injectedBean;
    }
}

The @Autowired annotation can even 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 allows dependencies to be declared final , guaranteeing they are set during object creation.

2.2 Setter‑based Injection

Here a setter method is annotated with @Autowired . Spring calls the setter after creating the bean with a no‑arg constructor or static factory method.

@Component
public class SetterBasedInjection {
    private InjectedBean injectedBean;

    @Autowired
    public void setInjectedBean(InjectedBean injectedBean) {
        this.injectedBean = injectedBean;
    }
}

As with constructor injection, the @Autowired annotation on the setter can be omitted.

public class SimpleMovieLister {
    private MovieFinder movieFinder;

    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }
    // business logic omitted...
}

2.3 Field‑based Injection

The field itself is annotated with @Autowired , and Spring sets it via reflection after the object is instantiated.

@Component
public class FieldBasedInjection {
    @Autowired
    private InjectedBean injectedBean;
}

Although concise, field injection has several significant drawbacks.

Drawbacks of Field Injection

3.1 Cannot Declare Immutable Fields

Fields marked final cannot be injected, so immutable dependencies require constructor injection.

3.2 Violates Single‑Responsibility Principle

Field injection hides the number of dependencies a class has, making it easy to accumulate many hidden dependencies and ignore the class’s growing responsibilities.

3.3 Tight Coupling to the DI Container

Classes that rely on field injection can only be instantiated by the Spring container (or via reflection), which hampers unit testing and reuse outside the container.

3.4 Hidden Dependencies

Because dependencies are not visible in constructors or setters, they are effectively hidden from the class’s public API, reducing clarity.

Conclusion

Field injection should be avoided. Prefer constructor injection for required, immutable dependencies and setter injection for optional ones. This approach improves immutability, testability, and adherence to SOLID principles.

References

Field injection is not recommended – Spring IOC by Marc Nuri

Spring official documentation: 1.4. Dependencies

JavaSpringdependency injectionConstructor InjectionField InjectionSetter Injection
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login 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.