Dependency Injection · interview question
In Spring Boot, how can you ensure that a bean is eagerly initialized at startup?
Multiple choiceHardchapter-9-curatedpremiumdependency-injectionconstructor-vs-field-injection
Answer and explanation
Correct answer
C. Use the @DependsOn annotation to specify other beans it relies on.
Why
The @DependsOn annotation can be used to specify that a bean should be initialized only after specified beans have been initialized, effectively ensuring its eager initialization if those other beans are also eagerly initialized.
Test yourself on Dependency Injection
A free test with instant, explained results. No account needed.
More Dependency Injection questions
- What advantage does constructor injection offer over field injection concerning encapsulation?
- In Spring Boot, how does lazy initialization of beans affect constructor injection?
- How does Spring Boot handle dependency injection when multiple beans match the type being injected?
- Which scope of a bean in Spring Boot is the default in Java?
- Why do teams use the @Autowired annotation in Spring Boot in production code?
- Why is constructor injection preferred over field injection in terms of immutability?