# Circular Dependencies in Spring and How to Fix Them

> Your Spring Boot app fails at startup with BeanCurrentlyInCreationException. What is a circular dependency and how do you resolve it?

Canonical: https://www.internhack.in/learn/interview/spring-boot-interview/spring-circular-dependencies
Last updated: 2026-11-28

Difficulty: Advanced

## Question

Your Spring Boot app fails at startup with BeanCurrentlyInCreationException. What is a circular dependency and how do you resolve it?

## Answer

You upgrade to a newer Spring Boot, run the app, and it refuses to start with a message about a cycle: the dependencies of some of the beans in the application context form a cycle. UserService needs OrderService, and OrderService needs UserService. Neither can be created first. This is a circular dependency, and the story around it teaches a lot about how Spring builds beans.

Here is why it is a problem. With constructor injection, Spring must have all constructor arguments before it can create a bean. To build UserService it needs an OrderService instance, but to build OrderService it needs a UserService. It is a chicken and egg situation with no valid starting point, so Spring throws BeanCurrentlyInCreationException. Historically, Spring could hide the cycle for singleton beans using setter or field injection: it created a raw, half-initialised instance, exposed an early reference through a three-level cache of singleton factories, and injected that partially built object into the other bean. It worked, but it meant beans could be handed references to objects not yet fully initialised, and it clashed with AOP proxies. Because of that fragility, Spring Boot 2.6 turned circular references off by default, and you get the failure unless you explicitly set spring.main.allow-circular-references=true.

The quick fixes exist, and you should know them. Annotate one of the injection points with @Lazy, so Spring injects a lazy-resolution proxy and the real bean is fetched on first use. Switch one side to setter injection. Inject an ObjectProvider or Provider and call getObject when needed. Allow circular references globally through the property. These make the error disappear but leave the design problem in place, so treat them as a temporary bandage and not as a cure.

The real fix is redesign, because a cycle is almost always a design smell: two classes know too much about each other, or responsibilities are misplaced. Several strategies help. Extract the shared logic into a third service that both depend on, so the arrow goes one way: UserService and OrderService both use a new UserOrderFacade or a lower-level component. Move the method that causes the back reference to the other class where it belongs. Use events: instead of OrderService calling UserService directly, publish an OrderCreatedEvent through ApplicationEventPublisher and let UserService listen, decoupling them. Introduce an interface at a lower layer, applying the dependency inversion principle so the high level policy does not depend on the details.

Diagnosing is straightforward: the exception message prints the full cycle path, listing each bean in order. Read it, find the weakest link, and break it there. Tools like IntelliJ's diagram of dependencies help in larger codebases. Architectural rules, for instance with ArchUnit, can enforce that layers depend only downward, preventing cycles from creeping in.

In an interview, connect this to the earlier discussion on constructor injection. Constructor injection surfaces cycles immediately, which is a feature: you learn about the design flaw during development instead of shipping it. Field injection hides the same flaw, which is one more reason teams prefer constructors.

## Code examples

### The cycle and two ways out

```
// Cycle: both need each other in constructors -> startup failure
@Service
class UserService {
    private final OrderService orders;
    UserService(OrderService orders) { this.orders = orders; }
}
@Service
class OrderService {
    private final UserService users;
    OrderService(UserService users) { this.users = users; }
}

// Band-aid: defer resolution with @Lazy
@Service
class OrderServiceLazy {
    private final UserService users;
    OrderServiceLazy(@Lazy UserService users) { this.users = users; }
}

// Proper fix: break the dependency with an event
@Service
@RequiredArgsConstructor
class OrderServiceDecoupled {
    private final ApplicationEventPublisher events;
    public void place(Order o) {
        // ... save order ...
        events.publishEvent(new OrderPlacedEvent(o.getUserId(), o.getId()));
    }
}

@Service
class UserServiceListener {
    @EventListener
    public void onOrderPlaced(OrderPlacedEvent e) {
        // update the user's loyalty points, no direct reference to OrderService
    }
}
```

@Lazy hides the cycle; publishing an event removes the dependency so the arrow points only one way.

## Key points

- Boot 2.6+ prohibits circular references by default.
- @Lazy, setter injection and allow-circular-references are band-aids, not fixes.
- Extract a third component, move responsibility, or use events to break cycles.
- Constructor injection makes cycles fail fast at startup.

## Common follow-up questions

- Why did Spring allow cycles with field injection earlier?
- What is the three-level singleton cache?
- How can ArchUnit prevent cycles?

## Interview tips

- Show that you treat cycles as a design smell, not just an error to silence.
- Give the event-based decoupling as your preferred fix.

## Concepts covered

Circular Dependency, BeanCurrentlyInCreationException, Lazy, Design Smell

Previous: [Testing Controllers with MockMvc and Mockito](https://www.internhack.in/learn/interview/spring-boot-interview/spring-mockito-mockmvc-testing) · [All of Spring Boot](https://www.internhack.in/learn/interview/spring-boot-interview)

## More Spring Boot questions

- [Inversion of Control and Dependency Injection Explained](https://www.internhack.in/learn/interview/spring-boot-interview/spring-ioc-dependency-injection)
- [Constructor vs Setter vs Field Injection](https://www.internhack.in/learn/interview/spring-boot-interview/spring-injection-types-constructor-preferred)
- [ApplicationContext vs BeanFactory](https://www.internhack.in/learn/interview/spring-boot-interview/spring-applicationcontext-vs-beanfactory)
- [Spring Bean Scopes and the Singleton Trap](https://www.internhack.in/learn/interview/spring-boot-interview/spring-bean-scopes)
- [Spring Bean Lifecycle Step by Step](https://www.internhack.in/learn/interview/spring-boot-interview/spring-bean-lifecycle)
- [@Component, @Service, @Repository and @Controller](https://www.internhack.in/learn/interview/spring-boot-interview/spring-stereotype-annotations)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Spring Boot](https://www.internhack.in/learn/interview/spring-boot-interview)
