0
0
Spring Bootframework~10 mins

@PostConstruct and @PreDestroy in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @PostConstruct and @PreDestroy
Bean Created
@PostConstruct Method Called
Bean Ready to Use
Application Context Closing
@PreDestroy Method Called
Bean Destroyed
Shows the lifecycle of a Spring bean: after creation, @PostConstruct runs once before use; before destruction, @PreDestroy runs once.
Execution Sample
Spring Boot
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class MyBean {
  @PostConstruct
  public void init() {
    System.out.println("Bean initialized");
  }

  @PreDestroy
  public void cleanup() {
    System.out.println("Bean destroyed");
  }
}
Defines a Spring bean with methods that run after creation and before destruction.
Execution Table
StepEventMethod CalledOutputBean State
1Bean instance createdNoneNoneCreated, not initialized
2@PostConstruct triggeredinit()"Bean initialized"Initialized, ready
3Bean used in applicationNoneNoneIn use
4Application context closingNoneNonePreparing to destroy
5@PreDestroy triggeredcleanup()"Bean destroyed"Destroyed
6Bean removed from contextNoneNoneRemoved
💡 Bean lifecycle ends after @PreDestroy method runs and bean is removed.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
Bean StateNot createdInitializedDestroyedRemoved
Key Moments - 2 Insights
Why does the @PostConstruct method run only once?
Because it runs right after the bean is created and before it is used, as shown in step 2 of the execution_table.
When exactly is the @PreDestroy method called?
It is called just before the bean is destroyed when the application context is closing, as shown in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the bean state after step 2?
AInitialized, ready
BCreated, not initialized
CDestroyed
DRemoved
💡 Hint
Check the 'Bean State' column at step 2 in the execution_table.
At which step does the @PreDestroy method run?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look for '@PreDestroy triggered' in the 'Event' column of the execution_table.
If the application context never closes, which method will NOT run?
A@PostConstruct
BBean constructor
C@PreDestroy
DBean usage
💡 Hint
Refer to the execution_table steps related to application context closing and @PreDestroy.
Concept Snapshot
@PostConstruct and @PreDestroy in Spring Boot:
- @PostConstruct runs once after bean creation, before use.
- @PreDestroy runs once before bean destruction.
- Used for setup and cleanup tasks.
- Methods must be void, no args.
- Lifecycle tied to Spring application context.
Full Transcript
In Spring Boot, beans have a lifecycle. When a bean is created, the method annotated with @PostConstruct runs once to initialize it. This happens before the bean is used in the application. Later, when the application is shutting down, the method annotated with @PreDestroy runs once to clean up resources before the bean is destroyed. This ensures proper setup and cleanup. The execution table shows these steps clearly: creation, initialization, usage, context closing, cleanup, and removal. Understanding this helps manage resources safely in Spring applications.