This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
The Bean life cycle is essentially Spring’s process from creation to destruction of a Bean. Here is a general outline of the interview process:
- BeanDefinition phase
- The scan resolution Bean class is BeanDefinitionRegistered in BeanDefinitionMap of BeanDefinitionRegistry
- Before creating beans, BeanDefinition merges are performed for those with class inheritance.
- BeanFactoryPostProcessor’s modification of BeanDefinition
- Bean instantiation stage: createBeanInstance
- This stage post-processing bean: InstantiationAwareBeanPostProcessor [postProcessBeforeInstantiation, postProcessAfterInstantiation]
- Instantiated before implementing InstantiationAwareBeanPostProcessor postProcessBeforeInstantiation, perform these post-processor instantiation previously defined method.
- SmartInstantiationAwareBeanPostProcessor# determineCandidateConstructors Spring will select the most optimal constructor to instantiate a bean.
- MergedBeanDefinitionPostProcessor# postProcessMergedBeanDefinition behind the main access to injected some annotation information automatically.
- SmartInstantiationAwareBeanPostProcessor# getEarlyBeanReference get exposure of objects in the l3 cache in advance
- Perform all InstantiationAwareBeanPostProcessor# postProcessAfterInstantiation
- Bean attribute assignment stage: populateBean
- Mainly perform InstantiationAwareBeanPostProcessor postProcessProperties method to Value the annotation to the @autowired @ assignment injection
- Bean initialization phase: initializeBean
- Check the Aware interface of the bean implementation in turn, and then call the corresponding aware method implementation in turn.
- Performs all the pre-processing of the BeanPostProcessor
- Perform the initialization method: InitializingBean interface, or XML configuration init-method method or PostConstruct annotation
- Performs all post-processing of the BeanPostProcessor
- Bean destruction phase
- Registration destruction Method
- Check to see if you have implemented the DisposableBean interface and other marked destruction methods
- Execute the destruction method.
If you need to be a little more abstract, Spring’s process of building a Bean can be divided into four parts: instantiation -> property assignment -> initialization -> destruction. This is the core process of the life cycle. Go through these four basic points and then expand on them.
The main thing we know about the life cycle is that Spring gives us some entry points to participate in the life cycle so that we have the flexibility to participate in some custom modifications to beans.
Some entry points for Spring are as follows:
- BeanFactoryPostProcessor
- Bean post-processor
- Initialization methods, etc
Not only can we use these pointcuts to achieve the customization we want, but Spring uses them extensively in implementing many mechanisms. I think understanding the lifecycle is more about these aspects.