Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does @Lazy do on a bean class, and what does it do on an injection point?

On a bean class or @Bean method, @Lazy tells Spring not to create the singleton at context refresh, but only when it is first needed. On an injection point, it makes Spring inject a lazy-resolution proxy, a stand-in object that looks up the real bean on its first method call. Spring Boot can make every bean lazy with spring.main.lazy-initialization=true, and in all cases the price is the same: configuration errors move from startup to the first request.

Why this question is asked

By default, Spring creates every singleton bean while the application starts. That is usually what we want, because a broken bean fails the startup instead of a customer's request. @Lazy changes this, and it means two quite different things depending on where we put it. Interviewers ask about it because the two meanings are often confused, and because lazy beans have real consequences for startup time and for when errors appear. In this answer, we will recap eager creation, see @Lazy on a bean, see @Lazy on an injection point and the proxy behind it, look at the global Spring Boot switch, and then at the trade-off all of them share.

1A Quick Recap: Singletons Are Created Eagerly

When a Spring application context refreshes, which is the moment it reads all configuration and builds the beans, it ends by calling preInstantiateSingletons(). That method walks every singleton bean definition and creates each one. So by the time the application says 'Started', every @Service, @Repository and @Component exists, with its dependencies injected and its @PostConstruct method already run.

This eager creation is a safety feature. A missing property, a wrong database URL or an unsatisfied dependency is reported immediately, and a broken deployment never starts. @Lazy is how we opt out of it for chosen beans.

2@Lazy on a Bean: Created on First Use

Placed on a class annotated with @Component (or @Service and so on), or on a @Bean method, @Lazy marks the bean definition as lazy-init. preInstantiateSingletons() skips it, and Spring creates it the first time somebody asks for it.

Java
@Component
@Lazy
class ReportEngine {
ReportEngine() { System.out.println("ReportEngine created"); }
String run() { return "report"; }
}
@SpringBootApplication
public class App {
public static void main(String[] args) {
var ctx = SpringApplication.run(App.class, args);
System.out.println("Context started"); // Context started
ctx.getBean(ReportEngine.class).run(); // ReportEngine created
ctx.getBean(ReportEngine.class).run(); // (nothing: same singleton)
}
}

Notice the order of the output. 'Context started' appears first, and the constructor runs only on the first getBean() call. The second call returns the same instance, because a lazy bean is still a singleton; only the moment of creation changed.

But there is a catch. If an eager bean injects ReportEngine directly in its constructor, Spring must create ReportEngine to build that eager bean, so it is created at startup after all. A lazy bean only stays lazy if nothing eager needs it, or if the injection point is lazy too. Putting @Lazy on a @Configuration class makes all of its @Bean methods lazy.

3@Lazy on an Injection Point: A Proxy Stands In

Placed on a constructor parameter, a field or a setter, @Lazy means something else. Spring does not look up the dependency at injection time at all. It injects a proxy, an object generated at runtime that implements the same type. The first time we call a method on the proxy, it asks the bean factory for the real bean, creating it if needed, and forwards the call.

Java
@Service
class InvoiceService {
private final ReportEngine reports;
InvoiceService(@Lazy ReportEngine reports) {
this.reports = reports;
System.out.println(reports.getClass().getSimpleName()); // ReportEngine$$SpringCGLIB$$0
}
String monthly() {
return reports.run(); // first call: ReportEngine created, then "report"
}
}
The two meanings of @Lazy
WhereWhat Spring doesWhen the real bean is created
On a @Component class or @Bean methodskips it in preInstantiateSingletonsfirst getBean or first eager injection
On a constructor parameter or fieldinjects a lazy-resolution proxyfirst method call on the proxy
On a @Configuration classmarks every @Bean method in it lazyfirst use of each bean
spring.main.lazy-initialization=truemarks every bean definition lazyfirst use of each bean

The class name shows that InvoiceService received a CGLIB subclass, not the real ReportEngine. For an interface type Spring uses a JDK dynamic proxy instead. This proxy is built by Spring's autowire candidate resolver, and it resolves the target on each call through the bean factory, which for a singleton returns the same instance every time.

So the two forms answer different questions. @Lazy on the bean says 'do not create me early'. @Lazy on the injection point says 'do not hand me the real object yet'. The second form is also how a constructor cycle can be broken, because the proxy can exist before the other bean does, though that usually hides a design problem.

4Making Everything Lazy in Spring Boot

Spring Boot 2.2 added a global switch. Setting spring.main.lazy-initialization=true in application.properties makes every bean definition lazy, as if each had @Lazy. Startup becomes faster because Spring only builds what the startup path actually touches. Web controllers, services and repositories are then created when the first request needs them.

Individual beans can opt back in with @Lazy(false) on the class or @Bean method, and a LazyInitializationExcludeFilter bean can exclude whole groups of beans by type. These are useful for beans that must run at startup, such as a scheduler or a message listener that would otherwise never be created because nothing calls it.

Java
# application.properties
spring.main.lazy-initialization=true
// Java: keep this one eager even with the global switch
@Component
@Lazy(false)
class StartupMigrationCheck { /* must run at startup */ }

5The Trade-off: Errors Move to the First Request

Every form of laziness moves work from startup to first use, and errors move with it. Suppose ReportEngine needs a property report.dir that is missing in production. With eager creation, the application fails to start and the deployment is rolled back. With a lazy ReportEngine, the application starts, passes its health check and takes traffic, and the first user who opens a report gets a 500 error with a BeanCreationException.

The first request also pays for the creation work: class loading, connection setup, cache building. That shows up as a slow first request. So laziness is a good fit for local development, tests and rarely used heavy beans, and a poor fit as a blanket setting for production services.

6Follow-up questions

Does @Lazy on a bean change its scope?

No. A lazy singleton is still a singleton: it is created once, on first use, and the same instance is returned afterwards. Prototype beans are never pre-instantiated anyway, so @Lazy on a prototype has no real effect.

Why can a lazy-resolution proxy fail at creation for some classes?

For a concrete class Spring creates a CGLIB subclass, so the class must not be final and the methods we call must not be final either. Final methods would run on the proxy itself, whose fields are empty. Injecting through an interface avoids the issue.

When would you choose ObjectProvider over @Lazy on an injection point?

When the dependency is optional or we want to decide at runtime whether to fetch it. ObjectProvider makes the lookup visible in the code with getObject() or getIfAvailable(), while @Lazy hides it behind a proxy that looks like a normal field.

7Conclusion

In this answer, we saw that Spring creates singletons eagerly during refresh, and that @Lazy opts out of this in two different ways. On a bean class or @Bean method it postpones creation until first use, unless an eager bean injects it directly. On an injection point it injects a proxy that resolves the real bean on its first method call. Spring Boot's spring.main.lazy-initialization=true makes every bean lazy, with @Lazy(false) as the exception, and all of these move configuration errors and creation cost from startup to the first request.

What to say in the interviewAbout 80 seconds aloud

Open with

@Lazy means two different things depending on where it sits. On a bean, so on a @Component class or a @Bean method, it marks the definition lazy-init, which means Spring skips it when it pre-instantiates singletons at the end of refresh and creates it the first time someone asks for it.

Then, if they want more

It is still a singleton. One catch is that if an eager bean injects it directly, it gets created at startup anyway.

On an injection point, like a constructor parameter or a field, Spring does not resolve the dependency at all; it injects a lazy-resolution proxy, a CGLIB subclass or a JDK proxy for an interface, and the proxy looks up the real bean on the first method call.

That is also how @Lazy can break a constructor cycle, though usually it hides a design problem. Spring Boot can make every bean lazy with spring.main.lazy-initialization=true, and I can keep particular beans eager with @Lazy(false).

The trade-off is always the same: startup gets faster, but bean creation errors and the creation cost move to the first request, so a broken configuration can pass the health check and fail for a real user.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 2.2
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.
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 @Lazy do on a bean class, and what does it do on an injection point?": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/5e80467222/