Challenge - 5 Problems
Conditional Bean Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when using @ConditionalOnProperty with missing property?
Consider a Spring Boot application with a bean annotated as
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true"). If the property feature.enabled is not set in application.properties, what happens to the bean?Spring Boot
@Configuration public class FeatureConfig { @Bean @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") public String featureBean() { return "Feature is enabled"; } }
Attempts:
2 left
💡 Hint
Think about how @ConditionalOnProperty behaves when the property is absent.
✗ Incorrect
The @ConditionalOnProperty annotation creates the bean only if the specified property exists and matches the given value. If the property is missing, the condition fails and the bean is not created.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax for conditional bean creation using @ConditionalOnClass
Which of the following bean definitions correctly uses
@ConditionalOnClass to create a bean only if DataSource.class is present on the classpath?Attempts:
2 left
💡 Hint
Check the official attribute name for @ConditionalOnClass.
✗ Incorrect
The correct attribute for @ConditionalOnClass is a varargs of Class> types, so using @ConditionalOnClass(DataSource.class) is correct. The other options use incorrect attribute names or syntax.
🔧 Debug
advanced2:00remaining
Why does this conditional bean not get created despite correct property?
Given the following code snippet, the bean
specialService is not created even though app.special.enabled=true is set in application.properties. What is the most likely cause?Spring Boot
@Configuration public class SpecialConfig { @Bean @ConditionalOnProperty(name = "app.special.enabled", havingValue = "true", matchIfMissing = false) public SpecialService specialService() { return new SpecialService(); } }
Attempts:
2 left
💡 Hint
Check for exact spelling and case sensitivity in property names.
✗ Incorrect
The @ConditionalOnProperty annotation requires the property name to exactly match the key in the properties file. If there is a typo or case mismatch, the condition fails and the bean is not created.
❓ state_output
advanced2:00remaining
What is the state of the application context with multiple conditional beans?
In a Spring Boot app, two beans are defined with these conditions:
Bean A: @ConditionalOnProperty(name = "feature.a.enabled", havingValue = "true")
Bean B: @ConditionalOnProperty(name = "feature.b.enabled", havingValue = "true")
If
feature.a.enabled=true and feature.b.enabled=false in application.properties, which beans exist in the application context?Attempts:
2 left
💡 Hint
Consider how each property value affects its bean creation independently.
✗ Incorrect
Each bean's creation depends on its own property. Since feature.a.enabled is true, Bean A is created. Since feature.b.enabled is false, Bean B is not created.
🧠 Conceptual
expert2:00remaining
Which condition causes a bean to be created only if another bean is missing?
You want to create a bean only if a bean of type
DataSource is NOT already defined in the Spring context. Which annotation achieves this?Attempts:
2 left
💡 Hint
Think about the annotation that checks absence of a bean.
✗ Incorrect
The @ConditionalOnMissingBean annotation creates the bean only if no bean of the specified type exists in the context. The other annotations check for presence or properties, not absence.