0
0
Spring Bootframework~8 mins

Bean concept in Spring in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: Bean concept in Spring
MEDIUM IMPACT
This concept affects application startup time and memory usage by managing object creation and lifecycle.
Managing object creation and lifecycle in a Spring application
Spring Boot
@Component
public class Service {
  private final Repository repo;

  public Service(Repository repo) {
    this.repo = repo;
  }
}

@Component
public class Repository {}
Spring container manages single instances (singleton scope by default), optimizing memory and startup.
📈 Performance GainReduces memory footprint and improves startup efficiency by reusing beans
Managing object creation and lifecycle in a Spring application
Spring Boot
public class Service {
  private final Repository repo = new Repository();
  // manual new object creation without Spring
}
Manually creating objects bypasses Spring's container, causing duplicated instances and no lifecycle management.
📉 Performance CostIncreases memory usage and startup time due to unmanaged objects
Performance Comparison
PatternBean CreationMemory UsageStartup TimeVerdict
Manual object creationMultiple unmanaged instancesHigh due to duplicatesLonger due to no container optimization[X] Bad
Spring-managed singleton beansSingle shared instanceOptimized minimal usageFaster due to container control[OK] Good
Eager loading many beansAll beans created at startupModerate to highSlower startup[!] OK
Lazy initialization beansCreated on demandLower initial memoryFaster startup[OK] Good
Rendering Pipeline
Spring's bean lifecycle affects application initialization, where beans are created, wired, and initialized before serving requests.
Bean Creation
Dependency Injection
Initialization
⚠️ BottleneckBean Creation stage can delay startup if many beans are eagerly created
Optimization Tips
1Use Spring-managed singleton beans to minimize memory and startup time.
2Enable lazy initialization to defer bean creation and speed up startup.
3Avoid manual object creation to prevent memory bloat and lifecycle issues.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using Spring-managed singleton beans?
AThey reduce memory usage by sharing instances
BThey increase startup time by creating many objects
CThey disable dependency injection
DThey force manual object creation
DevTools: Spring Boot Actuator and Java VisualVM
How to check: Use Actuator endpoints to check bean counts and VisualVM to monitor memory and CPU during startup
What to look for: Look for high memory usage or long startup times indicating inefficient bean management