Quick revision
The short answer
@Lazy on an injection point is legitimate when a dependency cycle is genuinely necessary between infrastructure beans, when it defers an expensive dependency that many requests never use, or, less ideally, to inject a bean's own proxy so that self-calls pass through @Transactional. It is hiding a problem when it breaks a cycle between business services, because that cycle shows mixed responsibilities. I would review every use, require a reason, and prefer ObjectProvider when the dependency is truly optional.
What to remember
In this answer, we saw that @Lazy on an injection point injects a proxy that resolves the bean on first use. That is a legitimate tool for inherent cycles between infrastructure beans and for expensive dependencies on rare paths, and an acceptable but inferior trick for transactional self-calls, where a separate bean is clearer. Between business services it hides a design problem that should be fixed by extracting a bean or using events. We ended with a review rule that asks for a reason for every use and prefers ObjectProvider for dependencies that are truly optional.
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.
@Lazy on an injection point is legitimate when a dependency cycle is genuinely necessary between infrastructure beans, when it defers an expensive dependency that many requests never use, or, less ideally, to inject a bean's own proxy so that self-calls pass through @Transactional. It is hiding a problem when it breaks a cycle between business services, because that cycle shows mixed responsibilities. I would review every use, require a reason, and prefer ObjectProvider when the dependency is truly optional.
Why this question is asked
@Lazy on a constructor parameter is a one-word fix that makes startup errors disappear, which is exactly why it spreads through codebases. At the fifteen-year level, interviewers want to see that we can tell a tool used on purpose from a tool used to silence a warning, and that we can set a rule a team can follow. In this answer, we will recap what @Lazy does at an injection point, go through the three legitimate uses with code, look at the case where it hides a design problem, compare it with ObjectProvider, and end with a simple review rule.
1A Quick Recap: What @Lazy Does at an Injection Point
When we put @Lazy on a constructor parameter or a field, Spring does not look up the dependency during injection. It injects a lazy-resolution proxy, an object generated at runtime that has the same type as the dependency. On the first method call, the proxy asks the bean factory for the real bean and forwards the call; later calls go through the proxy to the same singleton.
Two things follow from this. Because the proxy can be created before the real bean exists, @Lazy can break a constructor cycle. And because the real bean is created on first use, any error in creating it appears at that first call, not at startup. Every judgement below comes down to whether we want one of these effects for a good reason.
2Legitimate Use: A Necessary Cycle Between Infrastructure Beans
Some cycles are part of how infrastructure is wired, not a sign of tangled business logic. A typical example is configuration code where a bean that customises a framework component needs another bean that itself is built from that component. Spring's own code and popular libraries use @Lazy in exactly such spots.
Notice the features that make this acceptable. Both beans are infrastructure in one configuration area, the cycle is inherent in the problem rather than an accident of growth, and the alternative, splitting the provider apart, would make the code harder to read. Even here I would add a comment saying why @Lazy is there, so the next reader does not copy it as a general fix.
3Legitimate Use: Deferring an Expensive Optional Dependency
The second good use is a dependency that is expensive to create and used only on a rare path. Suppose a document service occasionally renders PDFs, and the PDF engine loads fonts and templates that take several seconds and a hundred megabytes.
Here laziness is a deliberate performance choice: startup stays fast and memory stays low on instances that never export a PDF. The cost is the one we already know, the first PDF export is slow and an error in the engine's configuration appears then. If that matters, we can warm it up after startup instead, or use ObjectProvider to make the deferred lookup visible.
4Acceptable but Not Ideal: Self-Injection for Transactional Self-Calls
A call from one method of a bean to another method of the same bean goes straight to this, so it skips the proxy and its @Transactional or @Async behaviour. One known workaround is to inject the bean into itself through a lazy proxy and call through that.
It works, because @Lazy lets a bean depend on itself without a creation cycle error. But it is a clever trick that surprises readers, and it suggests the class has two roles: orchestrating the import and processing one row. Moving importOne into a separate RowImporter bean is clearer and needs no annotation magic, so I would accept the self-injection only in code we are not otherwise changing.
5Hiding a Problem: Cycles Between Business Services
The case to push back on is the most common one. OrderService needs CustomerService, CustomerService needs OrderService, startup fails with a cycle, and someone adds @Lazy to one parameter. The application starts again, and the review looks harmless.
But the cycle was telling us that two services share a responsibility. With @Lazy in place, nothing changes in the design: the services still depend on each other, tests still need both, and the next developer can add more calls in both directions. The right fix is to extract the shared logic into a third bean, replace a callback with an event, or split a class that does two jobs. So in review I would treat @Lazy between two domain services as a request for a design discussion, not as a fix.
- Is it breaking a cycle?YesCheck which beans form the cycleNoCheck whether it defers real cost
- Are both beans infrastructure, cycle inherent?YesAccept with a commentNoBusiness cycle: redesign instead
- Is the dependency expensive and rarely used?YesAccept, or use ObjectProviderNoRemove @Lazy
- Optional dependency?prefer
ObjectProvider.getIfAvailable()
4 steps in order. Play them, or step through them one at a time.
6Prefer ObjectProvider When the Dependency Is Truly Optional
@Lazy is often used for dependencies that may not exist at all, for example a bean that is only present when a feature is enabled. @Lazy is a poor fit here, because the proxy is created anyway and the first call fails with NoSuchBeanDefinitionException if the bean is missing. ObjectProvider, a small Spring interface for looking up a bean on demand, says what we mean.
Notice the reader can see the lookup and the 'if available' in the code, so the deferred and optional nature is not hidden behind a field that looks like any other. ObjectProvider also offers getIfUnique() and stream() for the cases with several candidates.
7Follow-up questions
Why not just ban @Lazy on injection points?
Because a few uses are real tools, such as inherent infrastructure cycles and expensive rare dependencies. A ban would push people towards worse tricks. A review rule that asks for a reason and a comment keeps the good uses and stops the silent ones.
Does @Lazy on an injection point affect testing?
Yes. In a unit test without Spring we just pass a real or mock object, so nothing changes. In an integration test the proxy hides creation errors until the first call, so a test that never calls the lazy dependency will not notice it is broken. That is one more reason to keep its use rare.
How would you find existing uses in a large codebase?
A simple search for @Lazy in constructor parameters and fields gives the list. I would sort them by the kind of beans involved, fix the business-service cycles first and add comments to the ones we keep, possibly with an ArchUnit rule that flags new uses in domain packages.
8Conclusion
In this answer, we saw that @Lazy on an injection point injects a proxy that resolves the bean on first use. That is a legitimate tool for inherent cycles between infrastructure beans and for expensive dependencies on rare paths, and an acceptable but inferior trick for transactional self-calls, where a separate bean is clearer. Between business services it hides a design problem that should be fixed by extracting a bean or using events. We ended with a review rule that asks for a reason for every use and prefers ObjectProvider for dependencies that are truly optional.
Open with
I think of @Lazy on an injection point as having two effects: Spring injects a proxy, so it can break a creation cycle, and the real bean is created on first use, so errors and cost move to that first call.
Then, if they want more
It is legitimate when I want one of those effects on purpose. One case is a cycle that is inherent between infrastructure beans, for example a registry customiser that needs a provider which itself reads from the registry; I accept that with a comment.
Another is an expensive dependency used on a rare path, like a PDF engine that takes seconds and a lot of memory to build, where deferring it keeps startup fast. A third is self-injection, @Lazy on a field of the bean's own type so internal calls go through the transactional proxy.
It works, but a separate bean for the inner method is clearer, so I only tolerate it. Where it hides a problem is a cycle between business services, like OrderService and CustomerService, because the cycle shows mixed responsibilities and @Lazy leaves them tangled. There I ask for a redesign.
In review I want a reason for every use, and when the dependency is really optional I prefer ObjectProvider, because the code then shows the lookup openly.
About this answer
- Last updated
- 12 November 2026
- 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 "When is @Lazy on an injection point a legitimate design tool, and when is it hiding a...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/1177f69581/