In Spring lifecycle callbacks are manages by BeanPostProcessor implementations and several methods for extension are provided:
Bean Lifecycle Extensions
JSR-250 Annotations
Not a part of Spring core, must be included as a separate jar. Annotations @PostConstruct and @PreDestroy can be used to set lifecycle callbacks.
public class BeanLifecycleAnnotations extends BaseBean {
@PostConstruct
private void init() {
System.out.println("BeanLifecycle: PostConstruct");
}
@PreDestroy
private void destroy() {
System.out.println("BeanLifecycle: PreDestroy");
}
}
Init and Destroy methods
Spring specific configuration that can be used with XML and @Bean.
@Bean(initMethod = "init", destroyMethod = "destroy")
public BeanLifeCycleMethods beanLifeCycleMethods() {
}
Initialization callbacks
These are not recommended because they couple code to Spring implementation.
InitializingBean interface performs initialization work after the container has set all the necessary properties on the bean.
public class AnotherExampleBean implements InitializingBean {
@Override
public void afterPropertiesSet() {
// do some initialization work
}
}
DisposableBean interface allows a callback to the bean when the bean container is destroyed.
public class ExampleBean {
public void destroy() {
// do some destruction work (like releasing pooled connections)
}
}
Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:
- Methods annotated with
@PostConstruct
afterPropertiesSet()
as defined by theInitializingBean
callback interface- A custom configured
init()
method
Destroy methods are called in the same order:
- Methods annotated with
@PreDestroy
destroy()
as defined by theDisposableBean
callback interface- A custom configured
destroy()
method
Note: destroy callbacks are not invoked by prototype scope beans because there are not tracked by Application Context, their destruction and resource release bust be done manually.
References
https://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html
https://stackoverflow.com/questions/50681027/do-spring-prototype-beans-need-to-be-destroyed-manually