Bird
0
0

Which of the following is the correct way to use @PreDestroy in a Spring Boot bean?

easy📝 Syntax Q12 of 15
Spring Boot - Spring Annotations
Which of the following is the correct way to use @PreDestroy in a Spring Boot bean?
APlace @PreDestroy on a static method to run after application shutdown
BAnnotate a method with @PreDestroy to run cleanup code before bean destruction
CUse @PreDestroy on a constructor to initialize resources
DAnnotate the class with @PreDestroy to mark it for destruction
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct usage of @PreDestroy

    @PreDestroy is placed on a method to run cleanup code before the bean is destroyed.
  2. Step 2: Eliminate incorrect options

    @PreDestroy cannot annotate a class or constructor, and static methods are not lifecycle callbacks.
  3. Final Answer:

    Annotate a method with @PreDestroy to run cleanup code before bean destruction -> Option B
  4. Quick Check:

    @PreDestroy = method cleanup before bean removal [OK]
Quick Trick: Use @PreDestroy only on instance methods for cleanup [OK]
Common Mistakes:
  • Annotating the class instead of a method
  • Using @PreDestroy on constructors
  • Marking static methods with @PreDestroy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes