Why Field Injection Is the Least Recommended in Spring: A Deep Dive
This article examines Spring's three annotation-based dependency injection styles—field, constructor, and setter—showing why the widely used field injection is actually the least advisable, and explains the advantages of constructor injection introduced in Spring 4.x with concrete code examples and best‑practice guidelines.
Introduction
Spring's core features are IoC and AOP. Dependency injection is usually performed via annotations. Spring provides three main annotation styles, but the most commonly used one is actually the least recommended.
Common injection styles
Field injection
@Controller
public class FooController {
@Autowired
private FooService fooService;
}Simple injection: just add @Autowired or @Resource on the field.
Reduces a lot of boilerplate code, making the class look clean.
Adding a new field requires minimal code changes.
Constructor injection (Spring 4.x recommended)
@Controller
public class FooController {
private final FooService fooService;
private final FooService1 fooService1;
@Autowired
public FooController(FooService fooService, FooService1 fooService1) {
this.fooService = fooService;
this.fooService1 = fooService1;
}
}Code is more verbose.
Changing a field is harder because the constructor must be updated.
When more than five fields are needed, the constructor becomes bulky and violates clean design.
Setter injection (Spring 3.x recommended)
@Controller
public class FooController {
private FooService fooService;
@Autowired
public void setFooService(FooService fooService) {
this.fooService = fooService;
}
}Solves the heaviness of constructor injection.
Allows the class to be re‑configured or re‑injected after instantiation.
Why Spring 4.x prefers constructor injection
Dependencies become immutable by using final fields.
Null dependencies are detected at instantiation, causing an exception.
Enforces the single‑responsibility principle: many constructor parameters indicate the class has too many responsibilities.
Facilitates unit testing without loading the full Spring context.
Prevents potential NullPointerExceptions when the bean is used outside the IoC container.
Avoids circular dependencies.
Guarantees that the object returned to the client is fully initialized.
Handling many dependencies
If a class requires a large number of dependencies, it likely violates the single‑responsibility principle and should be split into smaller, more focused components.
Other reasonable injection approaches
All injection methods are valid depending on the concrete scenario. You can combine @Qualifier to narrow bean selection, mix setter and constructor injection, or apply other techniques as needed.
IDE support
IntelliJ IDEA provides plugins that assist with Spring injection annotations, offering code generation and inspection features.
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.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
