Bird
0
0

Why does this Spring Boot app fail to detect beans?

medium📝 Debug Q7 of 15
Spring Boot - Spring Annotations
Why does this Spring Boot app fail to detect beans?
public class App {
  @Bean
  public Service myService() { return new Service(); }
}

Assuming Service is a valid class, what is the likely cause?
AThe method name must start with 'get'
BThe @Bean method must be inside a @Configuration class
CThe Service class must be annotated with @Component
DThe @SpringBootApplication annotation disables @Bean methods
Step-by-Step Solution
Solution:
  1. Step 1: Understand where @Bean methods belong

    @Bean methods must be inside classes annotated with @Configuration or @SpringBootApplication (which includes @Configuration).
  2. Step 2: Check if the class is annotated properly

    The class lacks @Configuration or @SpringBootApplication, so the @Bean method won't be detected. Other options are incorrect.
  3. Final Answer:

    The @Bean method must be inside a @Configuration class -> Option B
  4. Quick Check:

    @Bean requires @Configuration class [OK]
Quick Trick: Place @Bean methods inside @Configuration classes [OK]
Common Mistakes:
  • Thinking @SpringBootApplication disables @Bean
  • Believing @Component is needed on Service for @Bean
  • Assuming method name affects bean creation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes