Spring Constructor Injection vs Field Injection vs Setter
Compare constructor, setter and field injection in Spring on immutability, testing, circular dependencies and required versus optional collaborators.

Spring can wire a collaborator into a bean in three ways: through the constructor, through a setter, or straight into a field. All three produce a working application, so the argument is never about whether the code runs. It is about what the class can promise afterwards, how it behaves in a unit test, and what a reader learns by looking at it. The comparison holds for Spring Framework 7 and Spring Boot 4, and nothing in it has changed in substance since Spring 4.3.
Constructor injection
@Service
class InvoiceService {
private final InvoiceRepository repository;
private final Clock clock;
InvoiceService(InvoiceRepository repository, Clock clock) {
this.repository = repository;
this.clock = clock;
}
}When a class has a single constructor, Spring uses it without any annotation. The fields are final, so the object is fully built the moment it exists and cannot be altered afterwards. Anyone who opens the class sees every dependency in one place, and a constructor with eight parameters announces that the class does too much, which is a design signal you would otherwise miss. The class can also be created correctly by plain Java: new InvoiceService(repo, clock) works in a test, in a script and in another framework.
Setter injection
@Service
class ReportService {
private Formatter formatter = new PlainFormatter();
@Autowired(required = false)
void setFormatter(Formatter formatter) {
this.formatter = formatter;
}
}A setter fits a dependency that is optional or has a sensible default, as above. It also allows a bean to be reconfigured after creation, which is rare in application code and occasionally useful in framework code. The costs are that the field cannot be final, and that nothing stops other code from calling the setter later and swapping a collaborator while the bean is in use. For an optional dependency, ObjectProvider<Formatter> is often cleaner than required = false, because it makes the optionality explicit in the type and avoids a null check.
Field injection
@Service
class OrderService {
@Autowired private OrderRepository repository;
@Autowired private Clock clock;
}This is the shortest form and the reason it fills older code bases. The costs are real, and they compound.
The dependencies are invisible in the signature, so adding a ninth does not look wrong, and classes quietly grow until nobody can describe what they do. Fields cannot be final, so the compiler cannot help you. A unit test cannot build the object with new, since the fields stay null, and so it needs ReflectionTestUtils, @InjectMocks or a Spring context to set them. And the class is only correct inside a container, which ties business code to a framework it should not know about.
Compare them in a test
@Test
void totalsOrders() {
var service = new InvoiceService(
new FakeRepository(),
Clock.fixed(Instant.EPOCH, ZoneOffset.UTC));
assertThat(service.totalFor(1L)).isEqualTo(Money.of(10));
}There is no framework, no reflection and no startup cost, and the fakes are ordinary classes. The equivalent test for the field-injected class needs extra machinery before the first assertion. Multiply that by a thousand tests and the suite is measurably slower, which is one reason the test pyramid puts plain unit tests at the base. A class that needs a container before it can be exercised pushes its tests up the pyramid, where they are slower and less precise.
What happens with circular dependencies
With constructors, a cycle between two beans cannot be built: A needs B to exist before it can be created, and B needs A. Startup fails with a message that names both beans, and the problem is found in the first run. Field and setter injection hide the cycle by creating one bean before it is complete and handing out a reference to it, so the application starts and fails in odd ways later. Spring Boot disables that behaviour by default, and the error you then see is explained in circular dependency. The better fix is a design without the cycle, usually by extracting the shared piece into a third class. Using @Lazy on one parameter breaks the cycle, and it is a workaround that leaves the design problem in place.
Summary table
| Constructor | Setter | Field | |
|---|---|---|---|
Fields can be final | Yes | No | No |
| Object is complete when created | Yes | No | No |
| Dependencies visible to a reader | Yes | Partly | No |
| Usable without Spring in tests | Yes | Yes | No |
| Detects cycles early | Yes | No | No |
| Good for optional dependencies | Awkward | Yes | No |
Converting a field-injected class
The change is mechanical, and the compiler checks it. Take a class with three @Autowired fields. Make each field private final, delete the annotations, and let the IDE generate a constructor with all three as parameters. Spring needs no annotation on a lone constructor. Run the tests: any test that built the object with new and set fields by reflection will now fail to compile, and each failure is a test that was relying on the half-built object. Fix them to pass real or fake collaborators, and they become faster as you go.
The one place the change can bite is a circular dependency that field injection had been hiding. If startup now fails with a message about a cycle, the old code was already fragile and was only working by luck of creation order. Break the cycle by moving the logic both beans share into a third bean that depends on neither. Resist the temptation to put @Lazy on the constructor parameter and move on, because it makes the symptom go away and the two classes still know too much about each other.
Do this one class at a time, as you touch each, and not as a single sweep across the code base. A sweep produces a huge diff that nobody reviews carefully, and the cycles it uncovers all arrive on the same day.
The case for field injection
It is acceptable in test classes, where @Autowired on the object under test is idiomatic and the class is never instantiated by your own code. It is acceptable in @Configuration classes, which Spring creates and nobody calls directly. Everywhere else in production code, prefer the constructor. If the constructor gets long, split the class. Switching the injection style to hide a long parameter list treats the symptom.
Reducing the typing
If boilerplate is the objection, Lombok's @RequiredArgsConstructor generates the constructor for every final field. It works, and it trades visibility for brevity: the constructor no longer appears in the source, so a reader must know the annotation. A hand-written constructor is five lines and is searchable. For a bean with two or three dependencies, writing it out costs little. Records can serve as injected configuration, though not as beans that need lifecycle callbacks.
When you have no choice
A framework that supports only field injection, or a base class you cannot change, may force the style on you. Isolate it: keep the field-injected class thin and delegate to a constructor-injected collaborator that holds the logic, so the logic stays testable. The rules in more depth are in how Spring injects dependencies. The official position is in the Spring reference on dependency injection, and the failures that follow from a missing bean are catalogued in unsatisfied dependency. The Spring blog carries the project's own guidance.


