Quick revision
The short answer
spring.main.lazy-initialization=true makes Spring create beans when they are first needed instead of all at startup, so the application starts faster. The costs are that configuration errors may appear only when a bean is first used, possibly in production, that the first requests are slower, and that memory needs are hidden until later, which makes heap sizing harder. Beans that must stay eager can be marked with @Lazy(false) or excluded with a LazyInitializationExcludeFilter.
What to remember
In this answer, we saw that spring.main.lazy-initialization=true makes Spring create beans on first use instead of at startup, which shortens startup by moving work later. The costs are that wiring and configuration errors can surface only in production, the first requests are slower, and memory grows after startup, which makes heap sizing harder. Beans that must stay eager can use @Lazy(false) or a LazyInitializationExcludeFilter, and lazy initialization suits development and some tests better than production.
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.
spring.main.lazy-initialization=true makes Spring create beans when they are first needed instead of all at startup, so the application starts faster. The costs are that configuration errors may appear only when a bean is first used, possibly in production, that the first requests are slower, and that memory needs are hidden until later, which makes heap sizing harder. Beans that must stay eager can be marked with @Lazy(false) or excluded with a LazyInitializationExcludeFilter.
Why this question is asked
Startup time is a daily cost for developers and a scaling cost in production, and lazy initialization looks like a free win: one property and startup drops by half. Interviewers ask about it because the property moves work and errors to a different moment rather than removing them. In this answer, we will recap what eager initialization does, see how lazy initialization changes it with a small example, go through each downside, and see how to keep chosen beans eager.
1A Quick Recap of Eager Initialization
By default, Spring creates every singleton bean during the context refresh, before the application is ready. That means every constructor runs, every dependency is injected, every @PostConstruct method runs and every proxy is created at startup. If anything is wrong, such as a missing bean, a bad property or a constructor that throws, the application fails to start, which is the safest moment to find out.
Eager creation is also where much of startup time goes. A service with 800 beans, some of which build Hibernate metadata, open connection pools or load templates, can spend most of its startup creating objects that the first request may never touch.
2What Lazy Initialization Changes
With spring.main.lazy-initialization=true, Boot marks every bean definition as lazy. Spring then creates a bean only when something asks for it: another bean that is being created, a request that reaches a controller, or code calling getBean. Dependencies that are injected into a lazy bean are created when that bean is created.
| Aspect | Eager (default) | Lazy |
|---|---|---|
| Startup time | Slower, all beans created | Faster, only needed beans |
| Wiring and config errors | Fail at startup | Fail on first use, maybe in production |
| First requests | Normal speed | Slower, beans created on demand |
| Memory after startup | Close to steady state | Grows later, harder to size heap |
| Good fit | Production | Development, some tests |
Notice where the line moved: from before the started message to the first request that needs the service. Nothing was saved overall; the work was moved. For a developer restarting the application thirty times a day, that is a real gain. For production, it changes when costs and failures appear.
3Downside 1: Errors Appear Late
The most serious downside is that configuration and wiring errors are no longer caught at startup. If a lazily created bean needs a property that is missing in production, the application starts, passes its readiness check and takes traffic. The error appears only when a request first needs that bean, perhaps hours later, perhaps for a feature used once a week.
In other words, lazy initialization trades a failed deploy, which is visible and safe because the old version keeps running, for a runtime error seen by a user. That is usually a bad trade in production.
4Downside 2: Slower First Requests and Hidden Memory
The work that left startup does not disappear. The first request to each part of the application pays for creating its beans: a controller, its services, repositories and perhaps the Hibernate EntityManagerFactory. A first request that normally takes 20 milliseconds can take several seconds, which can trigger client timeouts right after a deploy.
Memory is affected the same way. After an eager startup, the heap holds most of what the application will need, so a team can size the container and heap by looking at memory after startup. With lazy initialization, memory keeps growing as features are first used, so the measurement after startup is misleading and an out-of-memory error can appear days later, after a rarely used feature finally runs.
5Keeping Selected Beans Eager
Some beans must be created at startup even when everything else is lazy, for example a bean that schedules jobs, a listener that must be registered, or a check that should fail fast. There are two ways to keep them eager.
@Lazy(false) on a bean overrides the global setting for that bean. A LazyInitializationExcludeFilter bean excludes whole groups by type, which suits libraries that cannot annotate users' beans. Boot itself excludes some infrastructure this way, but checking which of our own beans need it is part of turning the property on.
6Follow-up questions
Is lazy initialization useful in tests?
It can speed up a large @SpringBootTest that only touches part of the application. But such a test then no longer proves that the whole context can start, so at least one test should run with eager initialization to catch wiring errors.
What is the difference between @Lazy on a bean and @Lazy on an injection point?
On a bean definition, it delays creating that bean until first use. On an injection point, Spring injects a lazy-resolution proxy, so the target is created only when a method is first called on it, which is also a way to break some circular dependencies.
Does lazy initialization reduce total memory use?
Only for beans that are never used. Beans that are used eventually are created eventually, so steady-state memory is about the same. The difference is when the memory is allocated, not how much.
7Conclusion
In this answer, we saw that spring.main.lazy-initialization=true makes Spring create beans on first use instead of at startup, which shortens startup by moving work later. The costs are that wiring and configuration errors can surface only in production, the first requests are slower, and memory grows after startup, which makes heap sizing harder. Beans that must stay eager can use @Lazy(false) or a LazyInitializationExcludeFilter, and lazy initialization suits development and some tests better than production.
Open with
By default Spring creates every singleton during startup. With spring.main.lazy-initialization=true, Boot marks all bean definitions as lazy, so a bean is only created when it is first needed, by another bean, by a request reaching a controller, or by getBean.
Then, if they want more
Startup gets faster because less is created up front, but the work is moved, not removed. That has three downsides. First, configuration and wiring errors that would have failed startup now appear when the bean is first used, possibly in production hours later, so a failed deploy becomes a user-facing error.
Second, the first requests to each feature are slower, because they pay for creating the controller, services and maybe the EntityManagerFactory, which can cause timeouts right after a deploy. Third, memory keeps growing as features are first used, so looking at memory after startup no longer tells me how to size the heap.
If I do use it, some beans must stay eager, like schedulers or checks that must fail fast, and I can mark them @Lazy(false) or exclude whole types with a LazyInitializationExcludeFilter bean. In practice I would use it for development and some tests, and keep eager initialization in production.
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 Boot reference
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 "What does spring.main.lazy-initialization=true do, and what are its downsides?": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/2071e3bcf7/