The life cycle of bean objects in Spring

Keywords: Java Spring xml Attribute

Spring provides some interfaces to provide some methods, reflecting the life cycle of bean objects in the spring container.
The specific process can be embodied as follows:

Read permission class name - > build a class object - > use this class object to build an object through the no parameter constructor newInstance().
                               ↓
Call set method to inject dependency
                               ↓
If the Bean has implemented the BeanNameAware interface
Call the setBeanName(String name) method it implements
The name value of the Bean in the Spring configuration file is passed here
                               ↓
If the Bean has implemented the BeanFactoryAware interface
The container calls the setBeanFactory method it implements
The parameter received by this method is the current container itself (other beans can be obtained in this way)
                               ↓
If the Bean has implemented the ApplicationContextAware interface
The container calls the setApplicationContext method, which is similar to step 4
                               ↓
If a class implements the BeanPostProcessor interface and injects it into the container
The overridden postProcessBeforeInitialization(Object bean, String beanName) in this class will be executed
This method is called before the init-method defined by itself.
                               ↓
Execute init method method to inject object definition into xml configuration file
                               ↓
If a class implements the BeanPostProcessor interface and injects it into the container
Then the overridden postProcessAfterInitialization(Object bean, String beanName) in this class will be executed.
This method is called after the init-method defined by itself.
                               ↓
When a Bean is no longer needed, it goes through the cleanup phase
If Bean implements the interface DisposableBean
Will call the destroy() method of its implementation
                               ↓
Finally, if the destroy method attribute is configured in the Spring configuration of the Bean
Will automatically call its configured destroy method

 

Design a class to test the life cycle of bean objects

/*This tests the complete life cycle of bean objects in the spring container.*/
public class Life implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,DisposableBean{

private String name;

public String getName() {
return name;
}
//1,The container creates this Bean Object
public Life() {
System.out.println("First step,create object");
}
//2,Here's the container Bean Object injection dependency
public void setName(String name) {
System.out.println("The second step,Dependency injection");
this.name = name;
}
//3,If this Bean It's done BeanNameAware Interface,The container will call it setBeanName(String)Method,What's passing here is Spring In profile Bean Of id value
@Override
public void setBeanName(String name) {
System.out.println("The third step,call setBeanName Method,because bean Class implements BeanNameAware");
}
//4,If this Bean It's done BeanFactoryAware Interface,The container will call it setBeanFactory Method
//The parameter received by this method is the current container itself(You can use this way to get other Bean)
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("The fourth step,call setBeanFactory Method,because bean Class implements BeanFactoryAware");
}

//5,If this Bean It's done ApplicationContextAware Interface,The container will call setApplicationContext Method,This step is similar to step 4,ApplicationContext yes BeanFactory Sub interface,There are more ways to do it
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
System.out.println("The fifth step,call setApplicationContext Method,because bean Class implements ApplicationContextAware");
}
//9,When Bean When no longer needed,Will go through the cleaning phase,If Bean Realized DisposableBean This interface,Will call the implementation destroy()Method;
@Override
public void destroy() throws Exception {
System.out.println("The ninth step,call destroy Method,because bean Class implements ApplicationContextAware");
}

/**
*Note that the following two methods are custom initialization and destruction methods
*When injecting objects, you need to specify init method =.., destroy method =... "
*Pay attention to the execution order of methods
*init-method Execute after each initialization method
*destroy-method At the end
*/
//7,If Bean stay Spring Configured in the configuration file init-method Property automatically calls its configured initialization method
//init-method="myInit"
public void myInit(){
System.out.println("The seventh step,call myInit Method,because bean stay xml The configuration in init-method Property to specify the initialization method");
}

//10,Last,If this Bean Of Spring Configured in configuration destroy-method attribute,Will automatically call its configured destroy method
public void myDestroy(){
System.out.println("The tenth step,call myDestroy Method,because bean stay xml The configuration in destroy-method Property to specify the initialization method");
}

}

 

Write another class to implement the BeanPostProcessor interface

/*Note: this class needs to be registered in the spring container to take effect*/
public class MyBeanPostProcessor implements BeanPostProcessor{

//This method is defined in the init-method Previous call
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("The sixth step,call postProcessBeforeInitialization Method,because spring Container registered BeanPostProcessor Implementation class of interface");
return bean;
}
//This method is defined in the init-method Then call
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("The eighth step,call postProcessAfterInitialization Method,because spring Container registered BeanPostProcessor Implementation class of interface");
return bean;
}

}

 

We make a configuration in the configuration file

<bean class="com.briup.ioc.life.MyBeanPostProcessor"></bean>

<bean name="life" class="Life" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="Zhang San"></property>
</bean>

The above is the complete life cycle, and the following is the incomplete life cycle. Generally speaking, there is no need to make an object have a complete life cycle method every time during development.

/*This tests the common declaration cycle of bean objects in the spring container.*/
/*Bean No interface provided by spring is implemented in, which is the case for most beans in later projects*/
public class LifeBean{
private String name;

public LifeBean(){
System.out.println("LifeBean() constructor");
}
public String getName() {
return name;
}

public void setName(String name) {
System.out.println("setName() Method");
this.name = name;
}

public void init(){
System.out.println("init Method execution");
}

public void destory(){
System.out.println("destory Method execution");
}

}

 

For singleton and non singleton objects, the non singleton objects will be out of the control of the container after being created
Object of single instance management:
1. By default, spring creates objects when reading xml files
2. Dependency injection, if any
3. The method specified in the init method =.. "property value will be called, if there is such a configuration.
4.Bean objects can be used normally
5. When the object is destroyed, the method specified in the destroy method =... "Property value will be called, if there is such a configuration.

Note 1: calling the container.destroy() method will destroy the singleton object
Note 2: lazy init = "true", this Bean object can be created when it is accessed for the first time, instead of reading the xml file.
Note 3: because it is a single instance, the object will only be created once


Non singleton managed objects:
1. When using this object, the spring container will create this object
2. Dependency injection, if any
3. The method specified in the init method =.. "property value will be called, if there is such a configuration.
4.Bean objects can be used normally

Note 1: the spring container does not destroy non singleton objects
Note 2: because it is a non singleton, a new Bean object will be created for each use
Non singleton is also loaded when the object is obtained, which will not be managed by the Spring container.

Posted by fatherlyons on Wed, 30 Oct 2019 19:40:29 -0700