Bird
0
0

Identify the issue in the following Spring Bean method:

medium📝 Debug Q6 of 15
Spring Boot - Inversion of Control and Dependency Injection
Identify the issue in the following Spring Bean method:
@Bean
public void createService() {
  return new Service();
}
AThe @Bean annotation is missing
BThe method has a void return type but returns an object
CThe method should be static
DThe method should not return a new instance
Step-by-Step Solution
Solution:
  1. Step 1: Check method return type

    The method is declared void but attempts to return an object, which is invalid.
  2. Step 2: Understand @Bean method requirements

    @Bean methods must have a return type matching the bean type and return an instance.
  3. Final Answer:

    The method has a void return type but returns an object -> Option B
  4. Quick Check:

    @Bean methods must return bean instances, not void [OK]
Quick Trick: @Bean methods cannot have void return type [OK]
Common Mistakes:
  • Using void return type with return statement
  • Thinking @Bean requires static method
  • Believing @Bean methods should not create new instances

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes