@Lazy Annotation — Deferred Bean Initialization
Fast recall
Quick revision
Core answer
@Lazy bean doesn't fail at startup but on first request, turning a deploy failure into a production incident. Default Spring behavior (eager) is usually better.Whether you know @Lazy defers creation but trades startup safety for startup speed, and whether you can identify its legitimate vs anti-pattern uses
Using @Lazy to 'fix' circular dependencies rather than redesigning; thinking @Lazy improves performance in general (it doesn't — it shifts work)
Explain why Spring's default eager mode is a deliberate correctness-over-speed choice; describe how CGLIB proxy interception works for @Lazy
Interview-ready guide
Interview answer
Spring's default behavior is eager initialization — all singleton beans are created at startup before the application handles any request. This is deliberate: configuration errors surface immediately instead of crashing on the first user request. @Lazy trades that safety for faster startup.
What it does
- The bean definition is still registered — Spring knows it exists
- Instead of creating the bean at startup, Spring injects a CGLIB proxy into dependents
- The real bean is created on the first method call through the proxy
The trade-off
- Faster startup — the expensive bean is not created until needed
- Deferred failure detection — a misconfigured
@Lazybean will not fail startup; it fails on the first request, a production failure instead of a deployment failure
Three placement levels give three different scopes
@Lazyon a@Configurationclass — every@Beanmethod inside becomes lazy by default@Lazyon a single@Beanmethod or class — only that bean is lazy@Lazyon an injection-point parameter — one consumer gets a proxy, other consumers still get the real bean eagerly; usually the right one
Global `spring.main.lazy-initialization=true`
- Flips the default for the whole application — every bean lazy unless annotated
@Lazy(false) - Useful for dev-loop restart speed (often 30–60% faster)
- Never enable in production — every configuration error now surfaces on first request instead of at boot
Interaction with `@PostConstruct`
- A
@Lazybean's@PostConstructruns on first use, not at startup - Side effects assumed to happen at boot (cache warmup, scheduler registration) are deferred
- The first request pays full init cost, inflating cold-start p99
The circular dependency trap
- The most common use of
@Lazyin production is breaking circular dependencies — but it masks the design problem - When someone removes the annotation during refactoring, the
BeanCurrentlyInCreationExceptionreturns with no explanation - Fix the design instead: extract the shared concern, use events, or apply interface segregation
Use @Lazy for genuinely expensive beans that are not always needed, at the injection-point parameter level for fine-grained control. Never use it as a permanent fix for circular dependencies, and never enable global lazy init in production.
@Lazy Annotation — Deferred Bean Initialization
Direct Answer
@Lazy defers a bean's creation from container startup to first actual use. Spring still registers the bean definition, but injects a CGLIB proxy wherever the bean is a dependency — the real instance is created on the first method call through the proxy. Three legitimate uses: faster startup for beans that may not be needed in every run (admin tools, batch jobs), breaking circular dependencies (by deferring one side's creation), and conditional initialization where the bean might be expensive and only used on some code paths. The cost: configuration errors surface late — a misconfigured @Lazy bean doesn't fail at startup but on first request, turning a deploy failure into a production incident. Default Spring behavior (eager) is usually better.
Common Mistakes & Pitfalls
Using @Lazy to 'fix' circular dependencies rather than redesigning; thinking @Lazy improves performance in general (it doesn't — it shifts work)
Check your understanding
Answer these without looking back at the article.
- How does @Lazy's CGLIB proxy differ from @Transactional's proxy?
- What goes wrong when @Lazy is used on a bean with @PostConstruct?
- Why is @Lazy on a @Configuration class (@Lazy @Configuration) different from @Lazy on a @Bean?
- When does @Lazy mask a design problem rather than solve one?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.