Video in progress

Strong Junior 🌱 Spring

What is a circular dependency, and what changed about it after Spring Boot 2.6?

Bean A depends on B, and B depends on A — that's a circular dependency. Since Boot 2.6 it's forbidden by default and the app fails at startup. The real fix is to fix the design.

📖 Answer

@Service class OrderService {
    OrderService(CustomerService c) { ... }
}
@Service class CustomerService {
    CustomerService(OrderService o) { ... }
}

With constructor injection these classes can't be created — neither one can exist without the other. Spring throws an error and shows the cycle:

┌─────┐
|  orderService
↑     ↓
|  customerService
└─────┘

With field injection Spring used to get away with it using half-built beans. Boot 2.6 made spring.main.allow-circular-references=false the default — a cycle usually points to a design mistake.

What you can do:

  • move the shared logic into a third class (often the best option);
  • publish an event instead of one calling the other (ApplicationEventPublisher);
  • as a temporary measure — delay the dependency with @Lazy or ObjectProvider.

Setting allow-circular-references=true just hides the problem.