Bird
0
0

Given this custom auto-configuration snippet, what bean will be created if no other bean of type MyService exists?

medium📝 component behavior Q4 of 15
Spring Boot - Advanced Patterns
Given this custom auto-configuration snippet, what bean will be created if no other bean of type MyService exists?
@Configuration
@ConditionalOnMissingBean(MyService.class)
public class MyAutoConfig {
  @Bean
  public MyService myService() {
    return new MyService("default");
  }
}
AAn error occurs due to missing bean
BA MyService bean with name 'myService' and value 'default'
CA MyService bean with name 'myService' and value 'custom'
DNo bean will be created
Step-by-Step Solution
Solution:
  1. Step 1: Understand @ConditionalOnMissingBean behavior

    The bean method runs only if no existing MyService bean is present.
  2. Step 2: Analyze bean creation

    Since no other MyService bean exists, Spring creates the bean named 'myService' with value 'default' as returned by the method.
  3. Final Answer:

    A MyService bean with name 'myService' and value 'default' -> Option B
  4. Quick Check:

    No existing bean + @ConditionalOnMissingBean = bean created [OK]
Quick Trick: Bean created only if missing, with method return value [OK]
Common Mistakes:
  • Assuming bean is not created when missing
  • Confusing bean name or value
  • Expecting runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes