0
0
Spring Bootframework~8 mins

Bean lifecycle overview in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Bean lifecycle overview
MEDIUM IMPACT
This affects application startup time and memory usage by controlling how and when beans are created and initialized.
Managing bean initialization timing for faster application startup
Spring Boot
@Component
@Lazy
public class HeavyService {
  public HeavyService() {
    // heavy initialization logic
  }
}

// Bean created only when first requested
Lazy initialization defers bean creation until actually needed, reducing startup time and spreading resource use.
📈 Performance GainReduces startup blocking time, improving perceived application readiness.
Managing bean initialization timing for faster application startup
Spring Boot
@Component
public class HeavyService {
  public HeavyService() {
    // heavy initialization logic
  }
}

// All beans eagerly created by default
All beans are created eagerly at startup, causing longer startup time and higher initial memory use.
📉 Performance CostBlocks application startup for all beans to initialize, increasing startup time by hundreds of milliseconds or more depending on bean complexity.
Performance Comparison
PatternBean Creation TimingStartup ImpactMemory UsageVerdict
Eager InitializationAll beans at startupHigh startup delayHigher initial memory[X] Bad
Lazy InitializationOn first useLower startup delayMemory spread over time[OK] Good
Rendering Pipeline
Spring container creates and initializes beans during application startup or on demand, affecting startup and runtime performance.
Bean Instantiation
Dependency Injection
Post-Processing
Initialization
⚠️ BottleneckBean Instantiation and Initialization during startup
Optimization Tips
1Avoid eager initialization for heavy beans to reduce startup time.
2Use @Lazy annotation to defer bean creation until needed.
3Monitor bean creation timing to identify startup bottlenecks.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance impact of eager bean initialization in Spring Boot?
ALonger application startup time
BSlower runtime response time
CIncreased network latency
DHigher CPU usage during user requests
DevTools: Spring Boot Actuator / Logs
How to check: Enable debug logging for Spring context startup or use Actuator endpoints to monitor bean creation timing.
What to look for: Look for timestamps showing when beans are created; early creation indicates eager loading, delayed creation indicates lazy loading.