Spring Context is also responsible for injection dependencies in the bean, either through setter or constructor methods or by spring autowiring.
Sometimes we want to initialize resources in the bean classes, for example creating database connections or validating third party services at the time of initialization before any client request. Spring framework provide different ways through which we can provide post-initialization and pre-destroy methods in a spring bean life cycle.
Life cycle callbacks
Post-initialization call back methods
Pre-destruction call back methods
Spring framework provides following 4 ways for controlling life cycle events of a bean:
InitializingBean and DisposableBean callback interfaces
*Aware interfaces for specific behavior
Custom init() and destroy() methods in bean configuration file
@PostConstruct and @PreDestroy annotations
InitializingBean and DisposableBean
The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container.
The InitializingBean interface specifies a single method:
InitializingBean.java
void afterPropertiesSet() throws Exception;
This is not a preferrable way to initialize the bean because it tightly couple your bean class with spring container. A better approach is to use “init-method” attribute in bean definition in applicationContext.xml file.
Similarly, implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed.
The DisposableBean interface specifies a single method:
DisposableBean.java
void destroy() throws Exception;
A sample bean implementing above interfaces would look like this:
package com.howtodoinjava.task;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class DemoBean implements InitializingBean, DisposableBean
{
//Other bean attributes and methods
@Override
public void afterPropertiesSet() throws Exception
{
//Bean initialization code
}
@Override
public void destroy() throws Exception
{
//Bean destruction code
}
}
*Aware interfaces for specific behavior
Spring offers a range of *Aware interfaces that allow beans to indicate to the container that they require a certain infrastructure dependency. Each interface will require you to implement a method to inject the dependency in bean.
Custom init() and destroy() methods
The default init and destroy methods in bean configuration file can be defined in two ways:
Bean local definition applicable to a single bean
Global definition applicable to all beans defined in beans context
Bean local definition
Local definition is given as below.
beans.xml
<beans>
<bean id="demoBean" class="com.howtodoinjava.task.DemoBean"
init-method="customInit"
destroy-method="customDestroy"></bean>
</beans>
. Global definition
Where as global definition is given as below. These methods will be invoked for all bean definitions given under <beans> tag. They are useful when you have a pattern of defining common method names such as init() and destroy() for all your beans consistently. This feature helps you in not mentioning the init and destroy method names for all beans independently.
<beans default-init-method="customInit" default-destroy-method="customDestroy">
<bean id="demoBean" class="com.howtodoinjava.task.DemoBean"></bean>
</beans>
Java program to show methods configured in bean XML configuration file.
DemoBean.java
package com.howtodoinjava.task;
public class DemoBean
{
public void customInit()
{
System.out.println("Method customInit() invoked...");
}
public void customDestroy()
{
System.out.println("Method customDestroy() invoked...");
}
}
@PostConstruct and @PreDestroy
Spring 2.5 onwards, you can use annotations also for specifying life cycle methods using @PostConstruct and @PreDestroy annotations.
@PostConstruct annotated method will be invoked after the bean has been constructed using default constructor and just before it’s instance is returned to requesting object.
@PreDestroy annotated method is called just before the bean is about be destroyed inside bean container.
Java program to show usage of annotation configuration to control using annotations.
package com.howtodoinjava.task;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class DemoBean
{
@PostConstruct
public void customInit()
{
System.out.println("Method customInit() invoked...");
}
@PreDestroy
public void customDestroy()
{
System.out.println("Method customDestroy() invoked...");
}
}
Spring Bean Life Cycle Important Points:
Spring Context is first using no-args constructor to initialize the bean object and then calling the post-init method.
The order of bean initialization is same as it’s defined in the spring bean configuration file.
The context is returned only when all the spring beans are initialized properly with post-init method executions.
When context is getting closed, beans are destroyed in the reverse order in which they were initialized i.e in LIFO (Last-In-First-Out) order.
Route of Spring Bean :
Aware Interfaces ( setBeanFactory() -> setBeanName() - > setApplicationContext())
-> Bean Post Processor (postProcessBeforeInitialization -> postProcessAfterInitialization)
- > InitializingBean and DisposableBean Callback Interfaces
-> Cuton Init & Destroy
References :
https://www.journaldev.com/2637/spring-bean-life-cycle
https://howtodoinjava.com/spring-core/spring-bean-life-cycle/
https://dzone.com/articles/spring-bean-lifecycle
No comments:
Post a Comment