@PostConstruct and @PreDestroy are part of the JSR250 specification and are not specific to Spring However they are commonly used in Spring applications
@PostConstruct is used on a method that needs to be executed after dependency injection is done to perform any initialization This method must have no arguments and return void
@PreDestroy is used on methods as a callback notification to signal that the instance is in the process of being removed by the container The method annotated with `@PreDestroy` is typically used to release resources that the instance has been holding
Here is an example:
import javaxannotationPostConstruct
import javaxannotationPreDestroy
@Component
public class ExampleComponent {
@PostConstruct
public void init() {
Systemoutprintln(ExampleComponent is initialized)
}
@PreDestroy
public void destroy() {
Systemoutprintln(ExampleComponent is about to be destroyed)
}
}
In this example the `init()` method is called right after the `ExampleComponent` bean is fully initialized and all its dependencies have been injected by the Spring container The `destroy()` method is called right before the Spring container removes this bean from the context.
@PostConstruct This could be used to set up any necessary state or to perform any configuration that needs to be done on the object after dependency injection has occurred For example you might use it to load data into a cache start a thread connect to a database etc
@PreDestroy This could be used to clean up any resources that the object has been holding before it is destroyed For example you might use it to close database connections stop threads release file handles etc
The @PostConstruct and @PreDestroy annotations are used to denote methods that should be called by the Spring container upon bean initialization and destruction respectively
@PostConstruct This annotation is used on a method that needs to be executed after the bean is fully initialized This method is called just after Spring has set all the properties (dependencies) on the bean It occurs after the afterPropertiesSet() method (from InitializingBean interface) and any declared init method.
@PreDestroy This annotation is used on methods as a callback notification to signal that the instance is in the process of being removed by the container The method annotated with @PreDestroy is typically used to release resources that the instance has been holding This method is called just before the Spring container removes the bean from the context It occurs before the destroy() method (from DisposableBean interface) and any declared destroy method
Here is the order of execution in the Spring Bean lifecycle:
- Bean Instantiation
- Populate Properties
- setBeanName()
- setBeanFactory()
- postProcessBeforeInitialization() (from BeanPostProcessor)
- @PostConstruct
- afterPropertiesSet() (from InitializingBean)
- Custom init method
- postProcessAfterInitialization() (from BeanPostProcessor)
- Bean is ready for use
- @PreDestroy
- destroy() (from DisposableBean)
- Custom destroy method
So @PostConstruct is called after all the bean properties have been set but before any custom initialization methods And @PreDestroy is called before any custom destroy methods
Post a Comment