Bird
0
0

How do you correctly define a Spring Bean using Java-based configuration?

easy📝 Syntax Q3 of 15
Spring Boot - Inversion of Control and Dependency Injection
How do you correctly define a Spring Bean using Java-based configuration?
A@Bean public MyService myService() { return new MyService(); }
B@Component public MyService myService() { return new MyService(); }
C@Service public void myService() { return new MyService(); }
D@Bean public void myService() { new MyService(); }
Step-by-Step Solution
Solution:
  1. Step 1: Recognize Java configuration style

    Java config uses @Bean on methods returning the bean instance.
  2. Step 2: Check method signature and return type

    The method must return the bean object; void return type is invalid.
  3. Final Answer:

    @Bean public MyService myService() { return new MyService(); } -> Option A
  4. Quick Check:

    @Bean methods must return the bean instance [OK]
Quick Trick: @Bean methods return the bean instance [OK]
Common Mistakes:
  • Using void return type in @Bean methods
  • Annotating methods with @Component instead of @Bean
  • Not returning the bean instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes