This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

The full life cycle of [single-instance bean] is discussed as the main line, which includes the following parts:

  • BeanDefinition part

    • BeanDefinitionThe resolution of
    • BeanDefinitionThe registration of
  • Bean instance section

    • Instantiation of a bean
    • Bean property assignment + dependency injection
    • The bean initialization process
    • Start and stop beans
    • The destruction of the bean

We can then declare the cycle of the Spring Bean as follows **** (method level **) : exactly the lifecycle of the instantiation part

1. The Spring container instantiates the Bean according to the instantiation strategy.

In instantiating beans, Spring uses a “policy pattern” to decide which method to use, typically reflection or CGLIB dynamic bytecode. By default, the Spring CglibSubclassingInstantiationStrategy.

2. After instantiation, if the bean has some properties set, use the set method to set some properties.

3. If the Bean implements the BeanNameAware interface, the #setBeanName(String beanName) method is called.

Aware interface is the core interface of Spring container, and it is a super interface with identification function. The bean that implements this interface has the ability to be notified by Spring container in the way of callback

4. If the bean implements the BeanClassLoaderAware interface, the setBeanClassLoader(ClassLoader ClassLoader) method is called.

5. If the bean implements the BeanFactoryAware interface, the setBeanFactory(BeanFactory BeanFactory) method is called.

6. If the vessel is registered BeanPostProcessor, will call # postProcessBeforeInitialization (Object beans, String beanName) method, finishing bean processing

The second phase of initialization is enhanced processing by the BeanPostProcessor, where the BeanPostProcessor processes all qualified instantiated bean objects in the current container. It effectively extends the bean instance object provided by the Spring container, allowing Spring to customize the bean during the initialization phase, such as handling the markup interface or providing a proxy implementation for it.

7. If the bean implements the InitializingBean interface, call the #afterPropertiesSet() method.

8. If the bean is configured with the init-method method, the specified method is called.

9. After initialization is complete, if the vessel is registered BeanPostProcessor will call # postProcessAfterInitialization (Object beans, String beanName) method, the complete rear bean processing.

10. The object completes initialization and the method call begins.

11. Before the container closes, if the bean implements the DisposableBean interface, call the #destroy() method.

12. Before the container closes, if the bean is configured with destroy-method, the method specified by it is called.


13. At this point, a bean completes its lifetime.

Summary:

First, the bean life cycle is divided intoBeanDefinitionPhases and bean instance phases.

1) BeanDefinition stage is divided into four parts: loading XML configuration file, parsing annotation configuration class, programmatic construction of BeanDefinition, and post-processing of BeanDefinition

2) The life cycle of the bean instance phase consists of four steps:

A) bean instantiation b) property assignment + dependency injection c) bean initialization lifecycle callbacks d) bean instance destruction

Note: To understand the life cycle of a bean, start with the BeanDefinition and then the bean instance