@PostConstruct and @PreDestroy help you run code right after a component starts and just before it stops. This is useful to set up or clean up resources automatically.
@PostConstruct and @PreDestroy in Spring Boot
@PostConstruct
public void init() {
// code to run after bean creation
}
@PreDestroy
public void cleanup() {
// code to run before bean destruction
}These annotations are placed on methods inside Spring-managed beans.
The methods should be void and take no arguments.
@PostConstruct
public void start() {
System.out.println("Bean is ready!");
}@PreDestroy
public void stop() {
System.out.println("Bean is going to be destroyed.");
}@PostConstruct
public void loadCache() {
cache.loadAll();
}
@PreDestroy
public void clearCache() {
cache.clear();
}This Spring component prints messages when it starts and stops. The @PostConstruct method runs after the bean is created. The @PreDestroy method runs before the bean is destroyed, such as when the app shuts down.
import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import org.springframework.stereotype.Component; @Component public class ResourceHandler { @PostConstruct public void openResource() { System.out.println("Resource opened."); } @PreDestroy public void closeResource() { System.out.println("Resource closed."); } }
Make sure your Spring application context is properly closed to trigger @PreDestroy methods.
These annotations require the bean to be managed by Spring (like using @Component or @Service).
From Spring 6 and Jakarta EE 9+, use jakarta.annotation.PostConstruct and jakarta.annotation.PreDestroy.
@PostConstruct runs code right after a Spring bean is ready.
@PreDestroy runs code just before the bean is removed.
Use them to manage setup and cleanup automatically in your Spring apps.
Practice
@PostConstruct annotation in a Spring Boot application?Solution
Step 1: Understand the role of @PostConstruct
@PostConstruct is used to run initialization code after the Spring bean is fully created and dependencies are set.Step 2: Compare with other lifecycle annotations
@PreDestroy runs before bean removal, and scheduling annotations run repeatedly, so they don't match @PostConstruct's purpose.Final Answer:
To run a method immediately after the bean is created and dependencies are injected -> Option AQuick Check:
@PostConstruct = run after bean creation [OK]
- Confusing @PostConstruct with @PreDestroy
- Thinking @PostConstruct runs before bean creation
- Assuming it schedules repeated tasks
@PreDestroy in a Spring Boot bean?Solution
Step 1: Identify correct usage of @PreDestroy
@PreDestroy is placed on a method to run cleanup code before the bean is destroyed.Step 2: Eliminate incorrect options
@PreDestroy cannot annotate a class or constructor, and static methods are not lifecycle callbacks.Final Answer:
Annotate a method with @PreDestroy to run cleanup code before bean destruction -> Option BQuick Check:
@PreDestroy = method cleanup before bean removal [OK]
- Annotating the class instead of a method
- Using @PreDestroy on constructors
- Marking static methods with @PreDestroy
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean is initialized");
}
@PreDestroy
public void cleanup() {
System.out.println("Bean is about to be destroyed");
}
}What will be printed when the application context starts and then shuts down?
Solution
Step 1: Understand @PostConstruct behavior on startup
The init() method runs after bean creation, printing "Bean is initialized" when the app context starts.Step 2: Understand @PreDestroy behavior on shutdown
The cleanup() method runs before bean destruction, printing "Bean is about to be destroyed" when the app context shuts down.Final Answer:
Bean is initialized
Bean is about to be destroyed -> Option CQuick Check:
Startup prints init, shutdown prints cleanup [OK]
- Mixing order of printed messages
- Expecting only one message to print
- Thinking @PreDestroy runs on startup
public class ResourceBean {
@PostConstruct
public void setup() {
System.out.println("Setup resources");
}
@PreDestroy
public void cleanup() {
System.out.println("Cleanup resources");
}
public void start() {
System.out.println("Start method called");
}
}When running the application, the developer notices that "Cleanup resources" is never printed on shutdown. What is the most likely cause?
Solution
Step 1: Check if bean is managed by Spring
@PostConstruct and @PreDestroy only work if Spring manages the bean lifecycle (e.g., via @Component or @Bean).Step 2: Understand why cleanup isn't called
If the bean is created manually (new keyword), Spring won't call lifecycle methods, so cleanup() won't run automatically.Final Answer:
The bean is not managed by Spring, so lifecycle annotations are ignored -> Option DQuick Check:
Lifecycle annotations need Spring-managed beans [OK]
- Thinking @PreDestroy must be static
- Assuming @PostConstruct blocks @PreDestroy
- Believing cleanup() runs without Spring management
@PostConstruct and @PreDestroy to achieve this?Solution
Step 1: Identify correct lifecycle method usage
@PostConstruct should open the connection after bean creation; @PreDestroy should close it before destruction.Step 2: Check code correctness
public class DbBean { private Connection conn; @PostConstruct public void open() { conn = DriverManager.getConnection("url"); System.out.println("Connection opened"); } @PreDestroy public void close() { conn.close(); System.out.println("Connection closed"); } } correctly uses @PostConstruct to open and @PreDestroy to close the connection with proper method calls.Final Answer:
public class DbBean { private Connection conn; @PostConstruct public void open() { conn = DriverManager.getConnection("url"); System.out.println("Connection opened"); } @PreDestroy public void close() { conn.close(); System.out.println("Connection closed"); } } -> Option AQuick Check:
Open after creation, close before destruction [OK]
- Swapping @PostConstruct and @PreDestroy methods
- Not calling connection methods inside lifecycle methods
- Missing lifecycle annotations on resource methods
Ready to prove it?
Pass the test and this pattern moves unknown
