Backend Development 8 min read

Understanding Spring Dependency Injection: Constructor, Setter, and Field Injection

This article explains why Spring Framework discourages field injection with @Autowired, compares constructor, setter, and field injection types, provides code examples for each, and discusses the drawbacks of field injection such as immutability issues, violation of single‑responsibility principle, tight coupling to the container, and hidden dependencies.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Spring Dependency Injection: Constructor, Setter, and Field Injection

Spring officially discourages the use of @Autowired for field or property injection, and many companies now forbid it in new projects. The warning appears starting with Spring 4.0, which recommends constructor injection and setter injection instead.

The framework supports three injection styles: constructor‑based, setter‑based, and field‑based. While field injection is common, IDEs and static analysis tools flag it as non‑recommended.

Constructor‑based injection marks the class constructor with @Autowired (which can be omitted) and lists required dependencies as parameters, allowing those fields to be declared final for immutability.

@Component
public class ConstructorBasedInjection {
private final InjectedBean injectedBean;
@Autowired
public ConstructorBasedInjection(InjectedBean injectedBean) {
this.injectedBean = injectedBean;
}
}

An equivalent example without the annotation shows that Spring can still inject the dependency via the constructor.

public class SimpleMovieLister {
private MovieFinder movieFinder;
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// business logic omitted
}

Setter‑based injection annotates a setter method with @Autowired . After a bean is instantiated (e.g., via a no‑arg constructor), Spring calls the setter to inject the dependency. The annotation can also be omitted.

@Component
public class SetterBasedInjection {
private InjectedBean injectedBean;
@Autowired
public void setInjectedBean(InjectedBean injectedBean) {
this.injectedBean = injectedBean;
}
}
public class SimpleMovieLister {
private MovieFinder movieFinder;
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// business logic omitted
}

Field‑based injection directly annotates a field with @Autowired . This approach keeps the code concise but has several drawbacks.

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

Drawbacks of field injection include the inability to declare final (immutable) fields, potential violation of the Single Responsibility Principle by hiding the number of dependencies, tight coupling to the Spring container (making the class hard to use outside the container or in unit tests), and hidden dependencies that are not visible in the class API.

Therefore, field injection should be avoided. Required dependencies are best injected via constructors, making them immutable, while optional dependencies can be injected via setters.

JavaSpringdependency injectionConstructor InjectionField InjectionSetter InjectionAutowired
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.