Performance: Conditional bean creation
MEDIUM IMPACT
This affects application startup time and memory usage by controlling which beans are created and loaded into the Spring context.
@Bean @ConditionalOnProperty(name = "feature.serviceA.enabled", havingValue = "true") public ServiceA serviceA() { return new ServiceA(); } @Bean @ConditionalOnProperty(name = "feature.serviceB.enabled", havingValue = "true") public ServiceB serviceB() { return new ServiceB(); }
@Bean
public ServiceA serviceA() {
return new ServiceA();
}
@Bean
public ServiceB serviceB() {
return new ServiceB();
}| Pattern | Beans Created | Startup Time Impact | Memory Usage | Verdict |
|---|---|---|---|---|
| Unconditional bean creation | All beans | High - creates all beans regardless of need | High - all beans consume memory | [X] Bad |
| Conditional bean creation | Only needed beans | Low - skips unnecessary beans | Low - less memory used | [OK] Good |