The lifecycle of a bean refers to the process from object creation to object destruction.

The process is as follows:

  1. Create a bean instance through a constructor (for example, a no-parameter construct).
  2. Set values for the bean’s properties and references to other beans (for example, call the set method).
  3. Calls the bean’s initialization method (which requires configuration initialization).
  4. The bean is ready to use (for example, the object is fetched).
  5. When the container is closed, the bean’s destruction method (which needs to be configured) is called.

A life cycle process example

Create a new class, Orders, and write the bean label in the configuration file. Then follow the steps above to write the printout in turn.

package com.pingguo.spring5.bean; Public class Orders {public Orders() {system.out.println (" Step 1: Execute the no-parameter constructor to create an instance of the bean "); } private String orderName; public void setOrderName(String orderName) { this.orderName = orderName; System.out.println(" Step 2: Call the set method to set the property value "); Public void initMethod() {system.out.println (" Step 3: Execute the initialization method "); } public void destroyMethod() {system.out.println (" Step 5: Execute the method "); }}Copy the code

Note:

  • The fourth step is achieved when called in the test method.
  • The initialization method and destruction method need to be configured in the configuration fileinit-methodanddestroy-method.
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! - the configuration initialization method, method of destruction - > < bean id = "orders" class = "com. Pingguo. Spring5. Beans. The orders" init - method = "initMethod" DestroyMethod ="destroyMethod"> </property> </bean> </beans>Copy the code

Now add a test function to execute it:

@Test public void test4() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml"); Orders orders = context.getBean("orders", Orders.class); System.out.println(" Step 4: Get the created bean instance object "); System.out.println(orders); // Manually tell the bean instance to destroy context.close(); }Copy the code

Note that there is the ClassPathXmlApplicationContext with, because of the close () method in this class. The ClassPathXmlApplicationContext is ApplicationContext implementation class.

Ok, execute test method test4():

The first step: perform a no-parameter constructor to create the instance of the bean Step 2: call set method set property values Step 3: implement initialization method Step 4: get create bean instance com object. The pingguo. Spring5. Bean. The Orders @ 43738 a82 step 5: Execute the destruction method Process Finished with exit code 0Copy the code

Second, a more complete process

There are actually two more steps to the more complete process, which is called the bean’s post-processor.

Are called before and after step 3 of the above 5 steps, so the 7 steps should now look like this:

  1. Create a bean instance through a constructor (for example, a no-parameter construct).
  2. Set values for the bean’s properties and references to other beans (for example, call the set method).
  3. A method that passes the bean instance to the postprocessor.
  4. Calls the bean’s initialization method (which requires configuration initialization).
  5. Another method to pass the bean instance to the postprocessor.
  6. The bean is ready to use (for example, the object is fetched).
  7. When the container is closed, the bean’s destruction method (which needs to be configured) is called.

Now look at the effect of adding a post-processor:

1. Create a post processor

Create a class that implements the interface BeanPostProcessor to create the postprocessor.

package com.pingguo.spring5.bean;

import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPost implements BeanPostProcessor {

}
Copy the code

Click on BeanPostProcessor and you will see that there are two methods that are called in succession during the process of the postprocessor:

  • PostProcessBeforeInitialization: initialization before the call
  • PostProcessAfterInitialization: call after initialization

Now manually implement these two methods in the class:

package com.pingguo.spring5.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPost implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object BeanName) throws BeansException {system.out.println (" Perform the method before initialization "); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {system.out.println (" Perform initialization method "); String beanName) throws BeansException {system.out.println (" Perform initialization method "); return bean; }}Copy the code

That’s not all. Now you need to configure the post-processor in the configuration file:

<! Rear - configuration processor - > < bean id = "myBeanPost" class = "com. Pingguo. Spring5. Beans. MyBeanPost" > < / bean >Copy the code

Once configured, all beans in the current configuration file will go through the post-processor.

Execute the test function:

This is the full life cycle.