0
0
Spring Bootframework~8 mins

@Configuration and @Bean in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: @Configuration and @Bean
MEDIUM IMPACT
This affects application startup time and memory usage by controlling how beans are created and managed in Spring's container.
Defining reusable service objects in Spring Boot
Spring Boot
@Configuration
public class AppConfig {
  @Bean
  public MyService myService() {
    return new MyService();
  }
}
Explicitly defines singleton bean creation, ensuring only one instance is created and reused.
📈 Performance GainReduces startup time and memory by avoiding duplicate bean instantiation
Defining reusable service objects in Spring Boot
Spring Boot
@Component
@Scope("prototype")
public class MyService {
  // service logic
}

// Multiple instances created if prototype scope or no singleton management
Using @Component with prototype scope can lead to multiple bean instances, increasing memory and startup cost.
📉 Performance CostIncreases memory usage and startup time due to redundant bean creation
Performance Comparison
PatternBean InstancesStartup TimeMemory UsageVerdict
@Component with prototype scopeMultiple instances possibleHigher due to redundant creationHigher due to duplicates[X] Bad
@Configuration with @BeanSingle instance per beanLower due to controlled creationLower due to reuse[OK] Good
Rendering Pipeline
Spring processes @Configuration classes during startup to create and register beans in the application context before serving requests.
Bean Creation
Dependency Injection
Application Startup
⚠️ BottleneckBean Creation stage can be slow if many beans are created redundantly or with complex initialization.
Optimization Tips
1Use @Configuration and @Bean to define singleton beans explicitly.
2Avoid multiple bean instances to reduce memory and startup time.
3Monitor bean creation with Spring Boot Actuator and profilers.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using @Configuration with @Bean in Spring Boot?
AEnsures singleton bean creation reducing memory and startup time
BAutomatically caches HTTP responses
CImproves database query speed
DReduces network latency
DevTools: Spring Boot Actuator and IDE Profiler
How to check: Enable Actuator endpoints and use IDE profiler to monitor bean creation count and memory usage during startup.
What to look for: Look for number of bean instances and startup duration; fewer beans and faster startup indicate better performance.