Bird
0
0

How can you define a Bean that depends on another Bean and ensure the dependent Bean is created first?

hard📝 Application Q9 of 15
Spring Boot - Inversion of Control and Dependency Injection
How can you define a Bean that depends on another Bean and ensure the dependent Bean is created first?
AUse @DependsOn annotation on the dependent Bean
BUse @Autowired only on the dependent Bean
CDeclare both Beans with @Component only
DUse @Primary on the dependent Bean
Step-by-Step Solution
Solution:
  1. Step 1: Understand Bean dependency ordering

    @DependsOn tells Spring to create specified Beans before the annotated Bean.
  2. Step 2: Differentiate from other annotations

    @Autowired injects dependencies but does not guarantee creation order. @Primary selects default Bean. @Component only registers Beans.
  3. Final Answer:

    Use @DependsOn annotation on the dependent Bean -> Option A
  4. Quick Check:

    Ensure Bean creation order = @DependsOn [OK]
Quick Trick: Use @DependsOn to control Bean creation order [OK]
Common Mistakes:
  • Assuming @Autowired controls creation order
  • Using @Primary to order Beans
  • Relying on @Component alone

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes