0
0
Spring Bootframework~20 mins

Conditional bean creation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Bean Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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";
    }
}
AThe bean is created regardless of the property value.
BThe bean is created with default value because the property is missing.
CThe bean is not created because the property is missing and does not match the required value.
DThe application fails to start due to missing property.
Attempts:
2 left
💡 Hint
Think about how @ConditionalOnProperty behaves when the property is absent.
📝 Syntax
intermediate
2: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?
A
@Bean
@ConditionalOnClass(className = "javax.sql.DataSource")
public MyBean myBean() { return new MyBean(); }
B
@Bean
@ConditionalOnClass(name = "javax.sql.DataSource")
public MyBean myBean() { return new MyBean(); }
C
@Bean
@ConditionalOnClass(classes = DataSource.class)
public MyBean myBean() { return new MyBean(); }
D
@Bean
@ConditionalOnClass(DataSource.class)
public MyBean myBean() { return new MyBean(); }
Attempts:
2 left
💡 Hint
Check the official attribute name for @ConditionalOnClass.
🔧 Debug
advanced
2: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();
    }
}
AThe property name in the annotation does not exactly match the property key in the properties file.
BThe matchIfMissing attribute should be true to create the bean.
CThe bean method is missing the @Component annotation.
DThe SpecialService class is not annotated with @Service.
Attempts:
2 left
💡 Hint
Check for exact spelling and case sensitivity in property names.
state_output
advanced
2: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?
AOnly Bean B is created; Bean A is not created.
BOnly Bean A is created; Bean B is not created.
CNeither Bean A nor Bean B is created.
DBoth Bean A and Bean B are created.
Attempts:
2 left
💡 Hint
Consider how each property value affects its bean creation independently.
🧠 Conceptual
expert
2: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?
A@ConditionalOnMissingBean(DataSource.class)
B@ConditionalOnBean(DataSource.class)
C@ConditionalOnProperty(name = "datasource.enabled", havingValue = "false")
D@ConditionalOnClass(name = "javax.sql.DataSource")
Attempts:
2 left
💡 Hint
Think about the annotation that checks absence of a bean.