Quick revision
The short answer
Spring Boot fails because OrderService needs CustomerService and CustomerService needs OrderService, so there is no order in which Spring can create them. Since Spring Boot 2.6, circular references are prohibited by default, and with constructor injection they could never be resolved anyway, because neither object can be built before the other exists. The real fix is in the design: move the shared logic into a third bean, or let one service publish an event the other listens to.
What to remember
In this answer, we saw that a dependency cycle means two beans need each other to be created. With constructor injection no creation order exists, and since Spring Boot 2.6 Boot also rejects the field and setter cycles that Spring could patch over. We fixed the design by extracting the shared query into a third bean, or by replacing a callback with an event, and we saw why turning on allow-circular-references or adding @Lazy only hides the problem.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Boot fails because OrderService needs CustomerService and CustomerService needs OrderService, so there is no order in which Spring can create them. Since Spring Boot 2.6, circular references are prohibited by default, and with constructor injection they could never be resolved anyway, because neither object can be built before the other exists. The real fix is in the design: move the shared logic into a third bean, or let one service publish an event the other listens to.
Why this question is asked
A dependency cycle is one of the first startup failures many of us meet after upgrading an older Spring Boot application, and it often appears in code that worked for years. Interviewers ask about it because the answer shows whether we understand how Spring creates beans, and whether we treat the error as a configuration nuisance or as a message about our design. In this answer, we will recap what a dependency cycle is, reproduce the failure, see why Spring cannot create the two beans, look at why Boot refuses even the cycles it could technically resolve, and then go through two fixes that remove the cycle instead of hiding it.
1A Quick Recap of How Spring Wires Beans
A bean is simply an object that Spring creates and manages for us. When a class is annotated with @Service, Spring creates one instance of it at startup (a singleton) and hands it to every other bean that asks for it. Asking for another bean is called a dependency, and the most common way to declare one is a constructor parameter. This style is called constructor injection.
To create a bean, Spring must first have every bean its constructor needs. So Spring builds a graph in its head: OrderService needs CustomerService, CustomerService needs AddressService, and so on. It then creates the beans from the leaves of that graph upwards. As long as the graph has no loops, there is always a bean that can be created next.
A cycle is a loop in that graph. The simplest cycle is two beans that need each other, which is exactly our case: OrderService needs CustomerService and CustomerService needs OrderService.
2Reproducing the Failure
Here are the two services. OrderService asks CustomerService whether a customer may order on credit, and CustomerService asks OrderService for the customer's open orders when it calculates a loyalty level.
Notice that the message names both beans and tells us the cycle is the problem, not a missing bean or a wrong configuration property.
3Why Constructor Injection Can Never Resolve This Cycle
Let us follow Spring as it tries. It starts with OrderService and looks at the constructor. To call it, it needs a CustomerService, so it pauses OrderService and starts creating CustomerService. That constructor needs an OrderService. But OrderService is not created yet; it is still waiting for CustomerService. Spring now sees that it is asked for a bean that is currently in creation, and it stops with a BeanCurrentlyInCreationException underneath the friendly Boot message.
The reason this cannot work is plain Java, not anything special about Spring. A constructor can only be called with real objects as arguments. To build A we need a finished B, and to build B we need a finished A. No ordering of the two calls exists. Even if we turned on every setting Spring offers, a pure constructor cycle would still fail.
- Create orderServiceits constructor needs
customerService - Pause orderService, create customerServiceits constructor needs
orderService - Is orderService already finished?YesInject it and continueNoIt is still in creation
- Fail with BeanCurrentlyInCreationExceptionBoot reports it as a dependency cycle
4 steps in order. Play them, or step through them one at a time.
4Why Spring Boot Refuses Even Resolvable Cycles
With field or setter injection, Spring can technically resolve a cycle. It creates OrderService with an empty constructor, then, before filling in its fields, it creates CustomerService and gives it a reference to the half-built OrderService. For years this worked silently, and many applications grew cycles without anybody noticing.
Since Spring Boot 2.6, the property spring.main.allow-circular-references is false by default, so Boot rejects these cycles too. The Spring team made this choice because a silently resolved cycle hides real risks: a bean can be used before its fields are set, and if one of the beans needs a proxy for @Transactional or @Async, the other bean may hold the raw object instead of the proxy. More importantly, a cycle almost always means two classes share a responsibility that belongs somewhere else.
5Fix 1: Extract the Shared Logic Into a Third Bean
Look at what each service really needs from the other. CustomerService does not need all of OrderService; it needs one fact, the number of open orders for a customer. That piece of knowledge can live in its own bean that both services use, and the cycle disappears because the arrows now point in one direction.
So the change is small, but it says something true about the domain: counting open orders is a query of its own, used by more than one service. This is the fix to try first.
6Fix 2: Let One Side Publish an Event
Sometimes the reverse call is a reaction rather than a question. For example, when an order is placed, CustomerService must update the customer's loyalty points. Instead of OrderService calling CustomerService and CustomerService calling back, OrderService publishes an event and CustomerService listens to it.
Notice that OrderService no longer knows CustomerService exists. Events suit notifications, where the publisher does not need an answer. If OrderService needs a result back, an event is the wrong tool and the third bean is the better fix. Also remember that a plain @EventListener runs in the same thread and transaction; @TransactionalEventListener runs after commit if that is what we want.
7Why @Lazy and the Property Only Hide the Problem
Two shortcuts make the error go away. We can set spring.main.allow-circular-references=true, which brings back the old behaviour for field and setter injection. Or we can put @Lazy on one constructor parameter, so Spring injects a proxy that finds the real bean later, on the first method call.
Both shortcuts leave the two services tangled. Tests stay harder because each service needs the other. We should use them only as a temporary step during an upgrade, with a ticket to remove them, and never as the fix.
8Follow-up questions
Does the same error happen with field injection?
Yes, since Spring Boot 2.6. With field injection Spring could resolve the cycle by injecting an early reference, but Boot sets spring.main.allow-circular-references to false by default, so the context fails with the same message. Setting the property to true would let field and setter cycles start again, while constructor cycles would still fail.
How do we find the cycle when it involves five beans instead of two?
The startup report lists every bean in the loop in order, so we read it from top to bottom. Then we look for the one arrow in that chain that should not exist, usually a lower-level service calling back up into a higher-level one, and we remove that arrow by extracting a bean or using an event.
9Conclusion
In this answer, we saw that a dependency cycle means two beans need each other to be created. With constructor injection no creation order exists, and since Spring Boot 2.6 Boot also rejects the field and setter cycles that Spring could patch over. We fixed the design by extracting the shared query into a third bean, or by replacing a callback with an event, and we saw why turning on allow-circular-references or adding @Lazy only hides the problem.
Open with
This error means OrderService needs CustomerService and CustomerService needs OrderService, so Spring has no order in which to create them. With constructor injection it is simply impossible, the same way plain Java cannot call two constructors that each need the other's finished object, so Spring throws BeanCurrentlyInCreationException and Boot reports it as a cycle.
Then, if they want more
Since Spring Boot 2.6, spring.main.allow-circular-references is false by default, so even field or setter cycles, which Spring used to resolve with an early reference, fail at startup. I would not switch that property back on or add @Lazy, because both keep the two services tangled. Instead I would look at what one service really needs from the other.
Usually it is a small piece of logic, like counting a customer's open orders, and I would move that into a third bean that both use, so the arrows point one way. If the reverse call is really a reaction, like updating loyalty points after an order, I would have OrderService publish an OrderPlaced event and let CustomerService listen.
I would keep the property or @Lazy only as a temporary step during an upgrade, with a ticket to remove it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 2.6, Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Startup fails with 'The dependencies of some of the beans in the application context...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/4cf3807cc2/