@Lazy Bean Initialization in Spring
Fast recall
Quick revision
Core answer
Knowledge of Spring's eager versus lazy initialization strategies
Overusing @Lazy to speed up startup without understanding it hides errors that eager initialization would catch
Explain the trade-off: @Lazy improves startup time but moves initialization errors from startup to runtime when the bean is first used
Interview-ready guide
Interview answer
A rarely used report client with expensive startup can be a reasonable lazy bean. The first call then pays creation cost and may reveal missing configuration, so latency and failure timing change. Putting @Lazy on one constructor parameter can allow two beans to be constructed through a proxy, but the runtime dependency cycle remains. Splitting responsibilities or introducing an event/callback boundary is usually clearer.
For example, a rarely used ReportGenerator can be lazy so it is created only on first use. The trade-off is that its initialization delay or configuration error also moves to that first request.
@Lazy Bean Initialization in Spring
Direct Answer
@Lazy on a bean definition requests deferred initialization until the bean is needed. @Lazy on an injection point can inject a lazy-resolution proxy. Non-lazy singleton beans are normally pre-instantiated when an ApplicationContext refreshes, while prototypes and other scopes already follow their own creation rules. @Lazy can break one construction-time cycle, but that often hides a design problem and moves failures to first use.
Code Example
@Component
@Lazy // not created at startup; built on first use
class ReportEngine {
ReportEngine() { System.out.println("Heavy init..."); }
}
@Service
class ReportService {
private final ObjectProvider<ReportEngine> engine;
ReportService(ObjectProvider<ReportEngine> engine) {
this.engine = engine; // ReportEngine still not built here
}
void run() {
engine.getObject().generate(); // built now, on first call
}
}
Common Mistakes & Pitfalls
- Lazy everywhere to fix slow startup: Freshers see a 15-second startup and annotate every bean with @Lazy. This masks the real problem (inefficient initialization logic) and moves errors to runtime. Fix the root cause instead—optimize database queries in @PostConstruct, remove unnecessary eager loading, or split heavyweight beans into separate modules.
- Not understanding proxy behavior: When you inject a @Lazy bean, Spring injects a proxy, not the real object. The first method call on that proxy triggers bean creation. Freshers sometimes serialize or reflect on the proxy and get confused by CGLIB classes in stack traces.
- Using @Lazy to break circular dependencies without fixing the design: Circular dependencies indicate poor design—two beans shouldn't depend on each other. @Lazy papers over the symptom but doesn't fix the coupling. Refactor one bean to use an event or callback instead of direct dependency.
Check your understanding
Answer these without looking back at the article.
- What is the difference between lazy and eager bean initialization?
- How does spring.main.lazy-initialization=true affect startup?
- What is the risk of using @Lazy?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.