Bird
0
0

How can you ensure a Spring bean's destroy method is called when the application context closes?

hard📝 Application Q9 of 15
Spring Boot - Inversion of Control and Dependency Injection
How can you ensure a Spring bean's destroy method is called when the application context closes?
AUse @PostConstruct annotation on the destroy method
BImplement InitializingBean interface
CSpecify the destroy method name in @Bean annotation or use @PreDestroy
DCall the destroy method manually in the code
Step-by-Step Solution
Solution:
  1. Step 1: Understand destroy method registration

    Destroy methods must be registered via @Bean(destroyMethod) or annotated with @PreDestroy.
  2. Step 2: Eliminate incorrect options

    InitializingBean is for init, manual calls are not automatic, and @PostConstruct is for init, not destroy.
  3. Final Answer:

    Specify the destroy method name in @Bean annotation or use @PreDestroy -> Option C
  4. Quick Check:

    Destroy method auto-call = @PreDestroy or destroyMethod [OK]
Quick Trick: Use @PreDestroy or destroyMethod to auto-call destroy [OK]
Common Mistakes:
  • Confusing init interfaces with destroy
  • Expecting manual calls
  • Misusing @PostConstruct

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes