0
0
Spring Bootframework~10 mins

Conditional bean creation in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Conditional bean creation
Start Application
Check Condition on Bean
Create Bean
Continue Application Startup
Spring Boot checks a condition before creating a bean. If the condition is true, the bean is created; otherwise, it is skipped.
Execution Sample
Spring Boot
@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public MyService myService() {
    return new MyService();
}
This code creates the MyService bean only if the property 'feature.enabled' is set to 'true'.
Execution Table
StepActionCondition CheckedCondition ResultBean Created
1Start application contextN/AN/ANo
2Evaluate @ConditionalOnProperty for 'feature.enabled'Is 'feature.enabled' == 'true'?TrueYes, create MyService bean
3Register MyService bean in contextN/AN/AYes
4Continue application startupN/AN/ABeans ready
5Application ready to useN/AN/AYes
💡 Bean creation depends on condition; if false, bean is skipped and not registered.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
feature.enabledundefinedtruetruetrue
MyService beannot createdevaluatedcreatedcreated
Key Moments - 2 Insights
Why is the MyService bean not created if the property 'feature.enabled' is false or missing?
Because @ConditionalOnProperty checks the property value at Step 2 in the execution_table. If the condition is false, the bean creation is skipped as shown in the flow.
Can multiple beans use different conditions to control their creation?
Yes, each bean can have its own @Conditional annotation with different conditions, so Spring evaluates each separately during startup.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the condition for bean creation evaluated?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Condition Checked' column in the execution_table.
If the property 'feature.enabled' is set to false, what happens to the MyService bean?
AIt is created anyway
BIt is not created
CIt is created but disabled
DApplication fails to start
💡 Hint
Refer to the 'Condition Result' and 'Bean Created' columns in the execution_table.
If you add another bean with a different condition, how does Spring handle bean creation?
AIt only creates the first bean with a true condition
BIt creates all beans regardless of conditions
CIt evaluates each bean's condition separately
DIt throws an error if multiple conditions exist
💡 Hint
See key_moments about multiple beans and conditions.
Concept Snapshot
Conditional bean creation in Spring Boot:
- Use @Conditional annotations (e.g., @ConditionalOnProperty)
- Spring checks condition during startup
- Bean is created only if condition is true
- Skips bean creation if condition is false
- Allows flexible, environment-based bean setup
Full Transcript
Conditional bean creation in Spring Boot means the framework decides whether to create a bean based on a condition. For example, using @ConditionalOnProperty, Spring checks if a property is set to a specific value. If true, it creates the bean; if false, it skips it. This happens during application startup. This approach helps to enable or disable features easily without changing code. Each bean can have its own condition, and Spring evaluates them one by one. This makes your application flexible and configurable.