Performance: Bean lifecycle overview
This affects application startup time and memory usage by controlling how and when beans are created and initialized.
@Component
@Lazy
public class HeavyService {
public HeavyService() {
// heavy initialization logic
}
}
// Bean created only when first requested@Component
public class HeavyService {
public HeavyService() {
// heavy initialization logic
}
}
// All beans eagerly created by default| Pattern | Bean Creation Timing | Startup Impact | Memory Usage | Verdict |
|---|---|---|---|---|
| Eager Initialization | All beans at startup | High startup delay | Higher initial memory | [X] Bad |
| Lazy Initialization | On first use | Lower startup delay | Memory spread over time | [OK] Good |
@Component
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
}cleanup() method is never called?
@Component
public class MyBean {
public void cleanup() {
System.out.println("Cleaning up");
}
}Pass the test and this pattern moves unknown