The dependencies of some of the beans in the application context form a cycle
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
This Spring error means two or more beans depend on each other in a loop, so the container cannot construct them cleanly. A simple example is A needing B in its constructor while B also needs A.
Newer Spring Boot configurations are less willing to resolve these cycles automatically, which is a good thing. Most circular dependencies indicate a design problem, not a missing annotation.
What a Circular Dependency Looks Like
The most obvious case is constructor injection in both directions:
Spring tries to build OrderService, sees it needs PaymentService, starts building PaymentService, then sees it needs OrderService again. At that point the context cannot make progress.
Why This Usually Signals a Design Problem
A circular reference often means responsibilities are mixed together. Instead of one service depending on another in a clear direction, both services are doing part of the same workflow and have become mutually aware.
Typical root causes:
- two services each coordinating business logic
- a helper component reaching back into its caller
- configuration classes calling each other's beans indirectly
- event publishing and handling collapsed into direct method calls
The best fix is usually to extract a third component that owns the shared behavior.
Refactor Toward One-Way Dependencies
Here is a safer redesign:
Now OrderService and PaymentService no longer depend on each other. CheckoutService coordinates the workflow, which gives the dependency graph a clear direction.
Workarounds and When To Use Them
You can sometimes break a cycle technically with @Lazy, setter injection, or ObjectProvider. Example:
This may get the application to start, but it should be treated as a narrow workaround, not the preferred design. Lazy injection defers the problem. It does not make the architecture clearer.
Also note that Spring Boot has disallowed circular references by default since the 2.6 line unless you explicitly opt in. Even when you can re-enable them, doing so usually hides a code smell.
How To Diagnose the Cycle
Read the exception carefully. Spring normally prints the path of the cycle. Then inspect the dependency chain and ask which component is responsible for orchestration versus low-level work.
Useful techniques:
- draw the dependency graph on paper
- search for constructor injections between the listed beans
- separate configuration from business services
- replace direct back-calls with events or a mediator component
Common Pitfalls
- Treating
@Lazyas the first fix instead of reconsidering the design. - Letting service classes depend on each other bidirectionally.
- Hiding the cycle in
@Configurationmethods rather than service constructors. - Re-enabling circular references globally without understanding the graph.
- Confusing orchestration logic with domain logic and spreading both across multiple beans.
Summary
- A bean cycle means Spring found a dependency loop it cannot construct cleanly.
- Constructor-to-constructor cycles are the easiest example, but indirect cycles happen too.
- The best fix is usually a refactor to one-way dependencies.
- '
@Lazyand similar techniques are workarounds, not ideal design.' - Use the exception path to find the cycle and extract a clearer orchestration boundary.
Related reading
- Thread Safe singleton class
- Unable to resolve ILogger from Microsoft.Extensions.Logging
- Understanding Python super with __init__ methods
- Understanding Spring @Autowired usage
- The difference between Executors.newSingleThreadExecutor.executecommand and new Threadcommand.start;
- The difference between the Runnable and Callable interfaces in Java
- Unity 2.0 How to use Resolve with ResolverOverride?
- Using a 'using alias class' with generic types?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseObject-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.