Sub topic 11
Eager and lazy initialization
This chapter assumes you know @Bean, @Component and constructor injection, and that you can run a plain Spring context. It covers when Spring creates a singleton bean: at startup (eager) or on first use (lazy).
The default: eager singletons
When an ApplicationContext refreshes, it creates every non-lazy singleton bean before the refresh finishes. Each of those beans goes through instantiation, injection and its initialization callbacks at that point.
This is deliberate. Wiring mistakes surface at startup, such as a missing dependency, a circular reference or a failing @PostConstruct. The cost is a slower startup, and memory is spent on beans that may never be used.
Prototype and web-scoped beans are not part of this. The container creates them only when something asks for one.
Making a bean lazy
Put @Lazy on a @Bean method or on a component class. The container then registers the definition but does not create the instance until the first request for it.
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
public class LazyDemo {
public static void main(String[] args) {
try (var ctx = new AnnotationConfigApplicationContext(Config.class)) {
System.out.println("context ready");
ctx.getBean(Report.class);
}
}
}
@Configuration
class Config {
@Bean
Cache cache() {
return new Cache();
}
@Bean
@Lazy
Report report() {
return new Report();
}
}
class Cache {
Cache() {
System.out.println("Cache created");
}
}
class Report {
Report() {
System.out.println("Report created");
}
}The output is:
Cache created
context ready
Report createdCache is created during refresh. Report is created by the getBean call. Its @PostConstruct method would run at that moment too.
Lazy at the injection point
@Lazy also works on an injection point. Spring then injects a lazy-resolution proxy instead of the real bean. The target is created the first time you call a method on the proxy.
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
class Dashboard {
private final Report report;
Dashboard(@Lazy Report report) {
this.report = report;
}
}This is useful when Report is expensive and Dashboard rarely needs it. It can also break a circular dependency, because neither side needs the other at construction time.
Making everything lazy
In Spring Boot, set spring.main.lazy-initialization=true. Every bean definition becomes lazy unless it is marked @Lazy(false).
flowchart TDThe trade-offs
| Setting | Startup time | Errors appear | First-use latency |
|---|---|---|---|
| Eager (default) | Slower | At startup | None |
| Lazy | Faster | On first use | Includes creation cost |
With global lazy initialization, a misconfigured bean can pass startup and fail on the first production request. Run integration tests with lazy mode off, or touch each bean once, so these failures show up before release.
Exercise
A singleton @Service named Mailer is marked @Lazy. A non-lazy @Component named Notifier takes Mailer as a plain constructor parameter. When is Mailer created, and what one change on Notifier would defer it?
Show answer
Mailer is created at startup, because Notifier is eager and needs a real Mailer to be constructed. Annotating the constructor parameter with @Lazy makes Spring inject a proxy, so the real Mailer is created on the first method call.