Constructor vs Field Injection
Choose the right injection style.
Constructor vs Field Injection is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Ways to Inject
Spring can inject dependencies three ways: through the constructor, into fields, or via setters. They all work, but they are not equal in quality.
The Spring team and community strongly recommend constructor injection. This lesson explains why.
Field Injection
Field injection places @Autowired directly on a field. It looks compact, but hides the dependency and complicates testing.
@Service
public class OrderService {
@Autowired
private OrderRepository repo;
@Autowired
private PricingService pricing;
}Constructor Injection
Constructor injection declares dependencies as constructor parameters. Spring supplies them when building the bean. Fields can be final, signaling true immutability.
@Service
public class OrderService {
private final OrderRepository repo;
private final PricingService pricing;
public OrderService(OrderRepository repo, PricingService pricing) {
this.repo = repo;
this.pricing = pricing;
}
}No @Autowired Needed
Since Spring 4.3, if a bean has a single constructor, @Autowired is optional. This keeps constructor-injected classes clean and annotation-light.
@Service
public class OrderService {
private final OrderRepository repo;
// single constructor: no @Autowired required
public OrderService(OrderRepository repo) {
this.repo = repo;
}
}Immutability and final
Constructor injection lets every dependency be final. The object is fully initialized once and never changes its collaborators, which prevents accidental reassignment and aids thread safety.
Field injection cannot use final, so the object can exist in a partially built state.
Testability Without Spring
A constructor makes dependencies explicit, so unit tests can build the object directly with mocks — no Spring context, no reflection.
@Test
void placesOrder() {
OrderRepository repo = mock(OrderRepository.class);
PricingService pricing = mock(PricingService.class);
OrderService svc = new OrderService(repo, pricing);
// ... arrange and assert
}Detecting Too Many Dependencies
A bloated constructor is a visible code smell. If a class needs eight collaborators, the long parameter list screams that it does too much. Field injection hides this and lets god-classes grow unnoticed.
Avoiding Circular Dependencies
Constructor injection surfaces circular dependencies at startup with a clear error, because two beans cannot both be constructed first. Field injection can mask the cycle until runtime, leading to subtle bugs.
A startup failure is the right time to discover and fix a design cycle.
Setter Injection
Setter injection suits genuinely optional dependencies that can change after construction. It is rarely needed; prefer constructor injection for required collaborators.
@Service
public class ReportService {
private Formatter formatter = new PlainFormatter();
@Autowired(required = false)
public void setFormatter(Formatter formatter) {
this.formatter = formatter;
}
}Less Boilerplate with Lombok
If constructors feel verbose, Lombok’s @RequiredArgsConstructor generates one from all final fields, giving constructor injection’s benefits with minimal code.
@Service
@RequiredArgsConstructor
public class OrderService {
private final OrderRepository repo;
private final PricingService pricing;
}Why the Recommendation Stands
Constructor injection wins on every axis:
- Immutable, fully initialized objects
- Trivial unit testing without a container
- Cycles and over-dependency become visible
- No reflection-only access for tests
Quick Check
Test your understanding of injection styles.
Recap
Prefer constructor injection.
- Enables
final, immutable dependencies - Single constructor needs no
@Autowired - Unit-testable without the Spring context
- Exposes circular dependencies and over-large classes
- Use setters only for optional, mutable collaborators
Frequently asked questions
Is the “Constructor vs Field Injection” lesson free?
Yes — the full text of “Constructor vs Field Injection” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.
What will I learn in “Constructor vs Field Injection”?
Choose the right injection style. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Microservices & REST APIs?
No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Constructor vs Field Injection” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?
Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Spring IoC Container
- Constructor vs Field Injection
- Bean Scopes
- Lifecycle Callbacks