Bird
0
0

How can you inject one bean defined by a @Bean method into another bean method within the same @Configuration class?

hard📝 Application Q9 of 15
Spring Boot - Spring Annotations
How can you inject one bean defined by a @Bean method into another bean method within the same @Configuration class?
ABy using @Autowired on the method parameters.
BBy calling the bean method directly inside the other @Bean method.
CBy creating a new instance manually inside the method.
DBy using @ComponentScan on the class.
Step-by-Step Solution
Solution:
  1. Step 1: Bean method calls

    Within a @Configuration class, calling one @Bean method inside another returns the Spring-managed bean.
  2. Step 2: Avoid manual instantiation

    Calling the method ensures Spring manages dependencies; manual new creates separate instances.
  3. Final Answer:

    By calling the bean method directly inside the other @Bean method. -> Option B
  4. Quick Check:

    Call bean methods inside @Configuration for injection [OK]
Quick Trick: Call @Bean methods inside each other for injection [OK]
Common Mistakes:
  • Manually instantiating beans instead of calling methods
  • Using @Autowired on method parameters inside @Configuration
  • Confusing @ComponentScan with injection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes