Quick revision
The short answer
I would treat the 200 cycles as design debt to be paid down steadily, not fixed in one big bang. First I would stop the number from growing with a test or build check, then fix cycles where code changes most often by splitting services or using events, use ArchUnit or module tools to find the package-level tangles behind the bean cycles, and track the count against a target date for turning allow-circular-references off.
What to remember
In this answer, we saw that 200 bean cycles are a symptom of tangled responsibilities that also makes upgrades and AOT harder. We first froze the count with a budget test that can only go down, then fixed cycles in frequently changed code by extracting beans, using events or splitting classes. We used ArchUnit to find the package cycles behind the bean cycles, and we tracked the count weekly against a target date for removing allow-circular-references.
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.
I would treat the 200 cycles as design debt to be paid down steadily, not fixed in one big bang. First I would stop the number from growing with a test or build check, then fix cycles where code changes most often by splitting services or using events, use ArchUnit or module tools to find the package-level tangles behind the bean cycles, and track the count against a target date for turning allow-circular-references off.
Why this question is asked
Large legacy codebases often reach this state quietly. Field injection let cycles start for free, the upgrade to Spring Boot 2.6 broke startup, and someone set spring.main.allow-circular-references=true to get the release out. Interviewers ask this at the fifteen-year level to see how we lead a long cleanup: how we judge whether it matters, how we stop the bleeding, how we choose what to fix first and how we keep a large team moving in the same direction. In this answer, we will look at why the cycles matter, how to freeze the count, how to prioritise, how to find the deeper package cycles and how to track progress to the day the setting is removed.
1Why 200 Cycles Matter Even If the App Runs
A bean cycle, two or more beans that need each other to be created, is a symptom. It tells us responsibilities are tangled: OrderService knows about InvoiceService, which knows about OrderService again. Code like this is hard to test on its own, hard to split into modules and easy to break, because a change in one class ripples around the loop.
There are concrete costs too. With the setting on, Spring hands out early references, unfinished objects, during startup, which can lead to beans used before they are ready or holding a raw object instead of a transactional proxy. Every future upgrade carries the risk that the setting is removed or behaves differently. And Spring AOT processing and GraalVM native images, which many teams now want for faster startup, work best with a clean, constructor-injected graph; cycles there tend to need special handling (check your version).
So I would explain to the team and to management that the goal is not purity. The goal is to be able to upgrade, test and split this code, and the cycles are a measurable proxy for how far we are from that.
2Step One: Stop the Number From Growing
Before fixing anything, I would make sure no new cycle can be added. Otherwise the team fixes ten while new features add twelve. The simplest check is a test that starts the context with the setting off for a list of known exceptions, but with 200 cycles a counting check is more practical: a test that inspects the bean graph, counts the cycles and fails if the count is above the current number.
BeanCycles here is our own small helper that builds a graph from factory.getDependenciesForBean(name) and counts strongly connected groups; it is about forty lines of code. Notice the budget is a constant in the test. Each pull request that fixes a cycle lowers it, so the number can only go down. This is sometimes called a ratchet.
3Step Two: Fix Cycles Where Change Is Frequent
Not all cycles are equal. A cycle between two stable classes nobody has touched in three years costs very little. A cycle in the checkout code, which ten developers change every sprint, costs us every day. So I would rank cycles by how often the classes in them change, which git history tells us in a few minutes, and by how central they are to upcoming work.
For each chosen cycle the fixes are the familiar ones. If one service asks the other a question, we extract the shared logic into a third bean that both depend on. If one service tells the other that something happened, we replace the call with an application event. If one class is simply doing two jobs, we split it. Each fix is small enough for a normal pull request, and I would ask teams to fix a cycle whenever they are already working in that code, rather than in a separate cleanup project that competes with features.
- List the cycles from the budget testeach with the beans involved
- Score by change frequencycommits in the last six months to those classes
- Is upcoming work planned in this code?YesFix it as part of that workNoLeave it in the queue
- Does one side only notify the other?YesReplace the call with an eventNoExtract a shared bean or split the class
- Lower the budget in the testthe number can only go down
5 steps in order. Play them, or step through them one at a time.
4Step Three: Find the Package Cycles Behind Them
Bean cycles usually sit on top of package cycles: the order package imports the billing package, and billing imports order. If we fix only the beans, new bean cycles grow back along the same package lines. ArchUnit, a library that tests architecture rules as ordinary unit tests, can find and forbid these, and tools like Spring Modulith can verify module boundaries in a Spring Boot application.
The first run will fail with a long report, so I would start with this rule recorded as known violations (ArchUnit has a freeze feature for this) and use the report as a map. Untangling the two or three biggest package cycles often removes dozens of bean cycles at once, which is a much better return than fixing beans one by one.
5Step Four: Track the Count and Set a Date
Finally I would make the progress visible. The budget number goes on the team dashboard, plotted every week. With 200 cycles and, say, five teams each fixing two cycles a week, we remove about 20 every two weeks, so we could reach zero in roughly five months. I would set a target date a little later than that, for example at the next planned Spring Boot upgrade, and agree that on that date the property is removed from application.properties.
Some cycles will be stubborn, often in infrastructure code. For the last few, a temporary @Lazy on one injection point with a comment and a ticket is acceptable, because it is local and visible, unlike a global setting that allows every cycle everywhere. Turning the setting off then becomes a small, safe change instead of a risky event.
6Follow-up questions
Would you just turn the setting off and fix everything that breaks?
Not with 200 cycles. The application would not start until every cycle was fixed, which means a long-lived branch, painful merges and pressure to ship quick hacks. A ratchet lets the team keep releasing features while the count goes down, and the final switch is safe because only a few cycles remain.
How do you get teams to spend time on this?
I tie it to work they want: faster tests, an easier upgrade, a module they want to split out. I also make the rule that cycles are fixed while working in that code, so the cost is spread across normal feature work, and I celebrate the budget going down on the dashboard.
Is @Lazy an acceptable fix?
Only for a handful of justified cases at the end, documented in the code. It keeps the cycle and moves errors to the first call. Used everywhere, it would just replace one global escape hatch with 200 local ones.
7Conclusion
In this answer, we saw that 200 bean cycles are a symptom of tangled responsibilities that also makes upgrades and AOT harder. We first froze the count with a budget test that can only go down, then fixed cycles in frequently changed code by extracting beans, using events or splitting classes. We used ArchUnit to find the package cycles behind the bean cycles, and we tracked the count weekly against a target date for removing allow-circular-references.
Open with
I would not try to fix all 200 cycles at once. First I would explain why they matter: they show tangled responsibilities, they make testing and splitting modules hard, and they put every future upgrade and any move to AOT or native images at risk.
Then, if they want more
Then I would stop the number from growing. I would add a test that counts the cycles in the bean graph and fails if the count goes above today's number, and every fix lowers that budget, so it works like a ratchet.
Next I would prioritise by cost: cycles in code that changes every sprint first, using git history to find them, and I would ask teams to fix a cycle when they are already working in that area, by extracting a shared bean, replacing a callback with an event, or splitting a class that does two jobs.
I would also run ArchUnit's beFreeOfCycles rule on top-level packages, because bean cycles usually sit on package cycles, and untangling a big package cycle removes many bean cycles at once.
Finally I would put the count on a dashboard, agree a target date, maybe the next Boot upgrade, and remove allow-circular-references then, with at most a few documented @Lazy exceptions left.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 2.6
- 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 "A large legacy codebase has 200 circular bean dependencies and runs with...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/0e3467927a/