1. Introduction
Understand the Spring bean life cycle of Spring container management, including the understanding of Spring container and IoC. Understand both the Spring bean lifecycle callback handler and the postprocessor. The Spring Framework provides several callback methods to create beans and some methods to destroy beans in the Spring IoC Container.
The Spring Framework provides several tag interfaces to change the behavior of beans in the container; They include InitializingBean and DisposableBean. Implementing these interfaces will cause the container to call afterpropertieset () for the former and destroy() for the latter to allow the bean to perform certain operations during initialization and destruction.
2. Understand Spring bean life cycle
The Spring bean lifecycle is easy to understand. When instantiating a bean, you may need to perform some initialization to make it available. Similarly, some cleanup may be required when beans are no longer needed and removed from the container.
Although there is a list of behind the scenes activities that occur between bean instantiation and destruction, only two important bean life cycle callback methods required for bean initialization and destruction are discussed here.
Beans can be notified after all properties are created and set, and before they are destroyed and removed from the bean container. This involves specifying the callback method to be called by the container. This is done in XML by specifying the attributes init method = "myinit" (for initializing callbacks) and destroy method = "mydestroy" (for destroying callbacks). "Myinit" and "cleanUp" are the names of the instance methods in the bean class.
Initialize callback
Implementing the org.springframework.beans.factory.InitializingBean interface allows the bean to perform initialization after the container sets all the necessary properties of the bean. The InitializingBean interface specifies only one method:
The org.springframework.beans.factory.InitializingBean interface provides an initialization callback method, as shown below
void afterPropertiesSet() throws Exception
Now you can implement the above interface and do some initialization functions in this method. As follows
public class Triangle implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("by Triangle call InitializingBean init method"); } }
In general, you can avoid using the InitializingBean interface (and it is discouraged because it unnecessarily couples code to Spring).
Java annotations can now also be used to declare bean lifecycle callbacks.
public class TriangleV2 { //init callback @PostConstruct public void myInit() { //Do some initialization here System.out.println("by Triangle call InitializingBean init method"); } }
Destroy callback
Implementing the org.springframework.beans.factory.DisposableBean interface allows the bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a method:
void destroy() throws Exception
Now you can implement the above interface and do some destruction actions in this method. As follows
public class Triangle implements InitializingBean, DisposableBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("by Triangle call InitializingBean init method"); } @Override public void destroy() throws Exception { //Do some destruction here System.out.println("by Triangle call DisposableBean destroy method"); } }
In general, you can avoid using the DisposableBean tag interface (and it is discouraged because it unnecessarily couples code to Spring)
Java annotations can now also be used to declare bean lifecycle callbacks.
public class TriangleV2 { //init callback @PostConstruct public void myInit() { //Do some initialization here System.out.println("by Triangle call InitializingBean init method"); } @PreDestroy public void cleanUp() { //Do some destruction here System.out.println("by Triangle call cleanUp method"); } }
If you use Spring's IoC container in a non web application environment; For example, in a rich client desktop environment; A close hook is registered with the JVM. This ensures a normal shutdown and calls the relevant destroy method on the singleton bean to free up all resources.