Why prefer constructor injection? How does Spring handle circular dependencies?
Short answer
Constructor injection gives final fields, an object that is valid from the moment it's created, and tests with plain new. With a cycle through constructors Spring can't create either bean first; since Boot 2.6 cycles are forbidden by default, and the right fix is the design.
3 points to say:
- Constructor injection:
final, valid immediately, explicit dependencies, testable without Spring. - A constructor cycle fails at startup; Boot 2.6+ forbids cycles by default, even with field/setter injection.
- Fix it by splitting a service or using events;
@Lazyis only a stopgap.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.