Take a look at the spring bean lifecycle:

1. Configuration stage of meta information

2. Meta-information analysis

3. Spring Bean registration

4. The merger of BeanDefinition

Load the Spring bean class

RootBeanDefinition---》Class
Copy the code

6-1. Before instantiation

InstantiationAwareBeanPostProcessor

6-2. Instantiation

CGLIB implements b, constructor injectionCopy the code

6-3. After instantiation

7. Attribute assignment

8. Aware interface callback stage

9-1. Before initialization

9-2. Initialization

9-3. After initialization

9-4. Initialization is complete

10-1. Before destruction

10-2, destroyed

Spring Bean GC

private final Map<String, Object> singletonObjects = new ConcurrentHashMap(256); — Level 1 cache

private final Map<String, Object> earlySingletonObjects = new HashMap(16); — Level 2 cache

private final Map<String, ObjectFactory<? >> singletonFactories = new HashMap(16); — Level 3 cache

Spring resolves loop dependencies between 6-2 instantiation and 7 attribute assignment;

The bean is a singleton and is being created, and the reference to the bean is put into the level 3 cache

After initialization, the cache is first accessed from level-1 cache, not from level-2 cache, and then from level-3 cache. After obtaining the cache, it is deleted from level-3 cache and put into level-2 cache. Why can’t the constructor injection loop dependency be resolved? Because instantiation requires constructor injection, it can’t be solved.

Cont: How many questions?

1. What does BeanFactoryPostProcessor do? 2, BeanDefinitionRegistryPostProcessor have what effect? 3. What does PriorityOrdered do? 4, What is the role of Ordered?Copy the code