0
0
Spring Bootframework~5 mins

@PostConstruct and @PreDestroy in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @PostConstruct annotation in Spring Boot?
It marks a method to be executed once immediately after the bean's initialization and dependency injection are complete.
Click to reveal answer
beginner
When is a method annotated with @PreDestroy called in a Spring Boot application?
It is called just before the bean is removed from the container, typically during application shutdown, to allow cleanup.
Click to reveal answer
intermediate
Can @PostConstruct and @PreDestroy methods have parameters?
No, methods annotated with @PostConstruct and @PreDestroy must have no parameters and return void.
Click to reveal answer
intermediate
How do @PostConstruct and @PreDestroy relate to the bean lifecycle in Spring Boot?
They provide hooks to run custom code right after bean creation (@PostConstruct) and right before bean destruction (@PreDestroy), managing setup and cleanup.
Click to reveal answer
intermediate
What happens if you forget to add @PreDestroy on a cleanup method in Spring Boot?
The cleanup code won't run automatically on shutdown, which may cause resource leaks like open connections or files.
Click to reveal answer
Which annotation runs a method after Spring Boot bean initialization?
A@Component
B@PreDestroy
C@Autowired
D@PostConstruct
What is the correct signature for a method annotated with @PreDestroy?
Apublic void cleanup()
Bpublic int cleanup(int code)
Cpublic String cleanup(String msg)
Dpublic void cleanup(String param)
When is a @PreDestroy method executed in Spring Boot?
ABefore bean destruction
BAfter bean initialization
CDuring bean creation
DWhen a bean is injected
What happens if you don't use @PostConstruct for initialization code?
AThe code runs before bean creation
BThe code runs after bean destruction
CThe code might not run automatically after bean setup
DThe code runs multiple times
Which of these is NOT true about @PostConstruct and @PreDestroy?
AThey help manage bean lifecycle events
BThey can have parameters in their methods
C@PostConstruct runs after dependencies are injected
D@PreDestroy runs before bean removal
Explain how @PostConstruct and @PreDestroy annotations help manage a Spring Boot bean's lifecycle.
Think about when you want to prepare and clean up resources in your app.
You got /4 concepts.
    Describe what could happen if you omit @PreDestroy on a method that closes resources in a Spring Boot app.
    Consider what happens if you never close a door after entering a room.
    You got /3 concepts.