Spring Boot Lifecycle Hooks
A brief intro into Spring Lifecycle hooks
In this article we will talking about Spring Boot life cycle hooks and where we would use them.
We will covering below topics,
-
What is a Bean in Spring Boot ?
Its an object that is created and managed by Spring IOC. Basically any object managed by
ApplicationContextclass of Spring is called a Bean. -
What is Bean Life Cycle ?
The spring Bean life cycle is as follows,
Spring IOC container started—>Bean Initialized—>Dependency injected—>custom init method—>custom utility methods—>destroy method. -
What are life cycle hooks ?
Life cycle hooks are functions that can be run before and after any of the above mentioned bean life cycle steps.
To understand Spring boot life cycle hooks better, lets divide the spring bean lifecycle into below phases,
Bean Creation Phase
-
Instantiation Phase
This phase happens just after
Bean initializedstep -
Populating Properties Phase
This phase happens just before
Dependency injectedstep -
Pre-Initialization Phase
This phase is done after
Dependecy injectedstep. HerepostProcessBeforeInitializationmethods gets called. Right after this@PostContructmethods gets called. -
AfterPropertiesSet Phase
This phase happens after
Dependecy Injectedstep but right beforePre-Initializationstep. -
Custom Initialization Phase
This phase basically happens during the
custom init methodlife cycle. Here the methods defined in theinitMethoddefined within@Beanis triggered. -
Post-Initialization Phase
After
Custom Initializationphase, this phase is triggered. HerepostProcessAfterInitializationmethod gets called.
Bean Destruction Phase:
Below phase starts getting executed , when a spring bean is been deleted by
ApplicationContext.-
Pre-Destroy Phase
Here Spring triggers
@PreDestroymethod. -
Destroy Phase
Here
@Destroymethod ofDisposableBeanis called. -
Custom Destruction Phase
Here method defined with
destroyMethodunder@Beanannotation is called.
Now lets look into some of the life cycle hooks that Spring Boot provides.
-
Lifecycle Hooks
-
Using interfaces
We can implement lifecycle hooks using interfaces. To implement lifecycle hooks using interfaces we need to implement
InitializingBeaninterface.Then any logic written within method
afterPropertiesSet()gets implemented.Example:
@Component class testBean implements InitializingBean { @Override public void afterPropertiesSet() { // Custom Logic } } -
Using JST-250 annotations
This is basically
@PreConstructand@PostDestroyannotation. We can use these method during initialization and destroy phase of Bean to implement any logic@Component class testBean { @PostConstruct public void postConstruct() { //... } @PreDestroy public void preDestroy() { //... } } -
Using
@BeanannotationsWe can also call methods during bean creation and bean destruction within
@Beanannotation.@Configuration class testBean { @Bean(initMethod = "onInitialize", destroyMethod = "onDestroy") public NewBean getNewBean() { return new NewBean(); } } -
Using BeanPostProcessor
We can also implement
BeanPostProcessorto trigger custom methods during and after bean creation. But this won’t be bean specific. For each and every bean in the application, implemented methods will be called.class TestBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // test logic return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // test logic return bean; } } -
Using Aware Interfaces
We can use
Awareinterfaces to hook to lifecycle of Bean.@Component class TestBean BeanNameAware, ApplicationContextAware { @Override public void setBeanName(String name) { // test logic } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // test logic } }
These are the different ways in which we can implement life cycle hooks in Spring Boot.
Some of real world use-cases of Spring Boot life cycle hooks are as follows,
- For logging purposes, wherein we might need to acquire bean names.
- If we suppose are using a bean that connects to a database and we need to to destroy the connection when the bean is destroyed, then in tha case,
@PreDestroymethod can be used.
For more articles on Java please check here.