Spring Framework
Dependency Injection
Circular Dependency
Application Context
Java Beans

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.

Practice OOD

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:

java
1@Service
2class OrderService {
3    private final PaymentService paymentService;
4
5    OrderService(PaymentService paymentService) {
6        this.paymentService = paymentService;
7    }
8}
9
10@Service
11class PaymentService {
12    private final OrderService orderService;
13
14    PaymentService(OrderService orderService) {
15        this.orderService = orderService;
16    }
17}

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:

java
1@Service
2class CheckoutService {
3    private final OrderService orderService;
4    private final PaymentService paymentService;
5
6    CheckoutService(OrderService orderService, PaymentService paymentService) {
7        this.orderService = orderService;
8        this.paymentService = paymentService;
9    }
10
11    public void checkout() {
12        orderService.createOrder();
13        paymentService.charge();
14    }
15}

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:

java
1@Service
2class PaymentService {
3    private final OrderService orderService;
4
5    PaymentService(@Lazy OrderService orderService) {
6        this.orderService = orderService;
7    }
8}

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 @Lazy as the first fix instead of reconsidering the design.
  • Letting service classes depend on each other bidirectionally.
  • Hiding the cycle in @Configuration methods 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.
  • '@Lazy and similar techniques are workarounds, not ideal design.'
  • Use the exception path to find the cycle and extract a clearer orchestration boundary.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD