We Removed Field Injection From 40 Services and Found 12 Circular Dependencies

2026-11-30来源 dev.toEnglish
We Removed Field Injection From 40 Services and Found 12 Circular Dependencies

The task was supposed to be cosmetic. Forty Spring services still used @Autowired on private fields, the team had agreed constructor injection was the better default, and someone with a quiet sprint offered to convert them. It is the kind of change that tends to go in without much discussion, because it only moves dependencies from one place in a class to another. The application stopped starting after the third service. Spring refused with a message every Spring developer has seen at least once: the dependencies of some of the beans in the application context form a cycle. By the end of the conversion there were twelve of those cycles, and none of them was new.

They had been there all along, and what hid them was a single line of configuration. Since version 2.6, Spring Boot refuses circular references by default, whatever the injection style, so a cycle between two field-injected beans fails at startup too. When the team upgraded past that version a year earlier, the application had indeed refused to start, and the fix at the time was spring.main.allow-circular-references=true, added with a comment promising to look into it later, but as always - nobody had looked into it later.

With that property switched on, Spring resolves a cycle between field-injected beans the old way: it creates the first bean, hands an early reference to the second, and fills in the field afterwards. Constructor injection cannot be rescued like that, with or without the property, because neither object can be built until the other already exists. So the conversion failed in exactly the places where the property had been covering for us, with both bean names in the error.

Field vs Constructor cycle

The first instinct during the conversion was @Lazy. Annotate one side of the cycle, Spring injects a proxy instead of the real bean, and the application starts again. It works, and it is the same move as the property: it does not remove the cycle, it defers the moment either side touches the other, and it adds a proxy whose behaviour surprises people later. We used it on two cycles to unblock the conversion and then went back to remove it.

Cycle Resolutions

Taking the twelve cycles apart one by one was more instructive than the conversion itself. Seven were two services that each needed a slice of the other's logic, and the fix was to extract that slice into a third, smaller service that both could depend on. Three were a method that simply lived in the wrong class, and moving it removed the dependency in one direction. Two were a service notifying another one of something that had happened, which became an application event, so the publisher no longer needed to know who was listening.

None of those changes was large. What made them possible was seeing them, and what had kept them invisible was one property in a configuration file, together with an injection style that let the property do its work. Every cycle had been in production for months, quietly making those services harder to test in isolation and harder to reason about, because a change to either side could affect the other in ways the class structure did not show.

The property is gone now, and so is the last @Lazy. On a current Spring Boot version that alone would bring the check back, field injection included. Constructor injection adds a guarantee on top: a cycle fails at startup even if someone turns the property back on during the next painful upgrade, which is exactly how we lost the check the first time.

More like this? I write short production war stories like this one, plus a deep-dive architecture series. Subscribe here if you want both in your inbox.

Recommended Resources for Java & Spring Boot Engineers

If you are preparing for Senior/Lead Java interviews or looking to solidify your Spring Boot & Architecture skills, check out these highly-rated resources from the Javarevisited publication (Use promo code friends20 for an exclusive 20% discount automatically applied at checkout):

原文:https://www.a8q9.cn/en/article/we-removed-field-injection-from-40-services-and-found-12-circular-dependencies-4d78c7.html

相关文章