Bird
Raised Fist0
Spring Bootframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using @ConditionalOnProperty in Spring Boot?
easy
A. To disable all beans in the application
B. To always create a bean regardless of configuration
C. To create a bean only if a specific property is set in the configuration
D. To create beans only during testing

Solution

  1. Step 1: Understand the role of @ConditionalOnProperty

    This annotation checks if a property is set in the application configuration.
  2. Step 2: Effect on bean creation

    If the property matches the condition, Spring Boot creates the bean; otherwise, it skips it.
  3. Final Answer:

    To create a bean only if a specific property is set in the configuration -> Option C
  4. Quick Check:

    Conditional bean creation = property check [OK]
Hint: Remember: @ConditionalOnProperty controls bean creation by config [OK]
Common Mistakes:
  • Thinking it always creates beans
  • Confusing it with disabling all beans
  • Assuming it only works in tests
2. Which of the following is the correct way to use @ConditionalOnProperty to create a bean only when feature.enabled is true?
easy
A. @ConditionalOnProperty(property = "feature.enabled", value = "true")
B. @ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
C. @ConditionalOnProperty(key = "feature.enabled", equals = "true")
D. @ConditionalOnProperty(enabled = "feature.enabled", isTrue = true)

Solution

  1. Step 1: Recall correct annotation parameters

    The correct parameters are name for the property key and havingValue for the expected value.
  2. Step 2: Match with options

    @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") uses name and havingValue correctly, others use invalid parameter names.
  3. Final Answer:

    @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") -> Option B
  4. Quick Check:

    Correct params = name + havingValue [OK]
Hint: Use 'name' and 'havingValue' parameters for @ConditionalOnProperty [OK]
Common Mistakes:
  • Using wrong parameter names like 'property' or 'key'
  • Confusing 'havingValue' with 'value'
  • Using boolean true without quotes
3. Given this Spring Boot bean definition:
@Bean
@ConditionalOnProperty(name = "app.feature", havingValue = "enabled")
public Service myService() {
    return new Service();
}

What happens if the property app.feature=disabled is set in application.properties?
medium
A. The bean myService is created and available
B. The bean is created but disabled
C. Application fails to start with error
D. The bean myService is NOT created

Solution

  1. Step 1: Understand the condition

    The bean is created only if app.feature equals "enabled".
  2. Step 2: Check the property value

    The property is set to "disabled", which does not match "enabled".
  3. Final Answer:

    The bean myService is NOT created -> Option D
  4. Quick Check:

    Property mismatch = no bean creation [OK]
Hint: Bean creates only if property matches havingValue exactly [OK]
Common Mistakes:
  • Assuming bean creates regardless of property value
  • Thinking app fails to start on mismatch
  • Believing bean is created but inactive
4. Consider this bean definition:
@Bean
@ConditionalOnProperty(name = "service.enabled", havingValue = "true")
public Service service() {
    return new Service();
}

Why does the bean never get created even when service.enabled=true is set in application.properties?
medium
A. The property key name is incorrect or misspelled
B. The property is a string but havingValue expects a boolean
C. The bean method is missing @Component annotation
D. The bean method must be static to work with @ConditionalOnProperty

Solution

  1. Step 1: Verify property key spelling

    If the property key in application.properties is misspelled or different, condition fails.
  2. Step 2: Check other options

    HavingValue compares strings, so boolean type mismatch is not an issue; @Component is not needed on @Bean methods; static is not required.
  3. Final Answer:

    The property key name is incorrect or misspelled -> Option A
  4. Quick Check:

    Property key must match exactly [OK]
Hint: Check property key spelling carefully for conditional beans [OK]
Common Mistakes:
  • Assuming boolean type mismatch causes failure
  • Thinking @Component is needed on @Bean methods
  • Believing bean methods must be static
5. You want to create two beans of the same type but only one should be active based on a property mode which can be "simple" or "advanced".
Which is the best way to define these beans using @ConditionalOnProperty?
hard
A. Use @ConditionalOnProperty(name = "mode", havingValue = "simple") on the simple bean and @ConditionalOnProperty(name = "mode", havingValue = "advanced") on the advanced bean
B. Use @ConditionalOnProperty(name = "mode") on both beans without havingValue
C. Create both beans without conditions and select one at runtime
D. Use @ConditionalOnProperty(name = "mode", matchIfMissing = true) on both beans

Solution

  1. Step 1: Understand the requirement

    Only one bean should be active depending on the property value "simple" or "advanced".
  2. Step 2: Apply conditional annotations

    Using @ConditionalOnProperty with havingValue set to "simple" or "advanced" on each bean ensures only one bean loads.
  3. Step 3: Evaluate other options

    Use @ConditionalOnProperty(name = "mode") on both beans without havingValue lacks value check, so both beans may load; Create both beans without conditions and select one at runtime loads both always; Use @ConditionalOnProperty(name = "mode", matchIfMissing = true) on both beans activates both if property missing.
  4. Final Answer:

    Use @ConditionalOnProperty(name = "mode", havingValue = "simple") on the simple bean and @ConditionalOnProperty(name = "mode", havingValue = "advanced") on the advanced bean -> Option A
  5. Quick Check:

    Use havingValue to select one bean by property [OK]
Hint: Use havingValue to pick one bean per property value [OK]
Common Mistakes:
  • Not specifying havingValue causing both beans to load
  • Trying to select bean at runtime instead of conditionally
  • Using matchIfMissing on both beans causing conflicts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard