Spring boot process

Spring boot process:

1. Spring loads the information in the "bean configuration file" into the "BeanDefinitionRegistry" of the container, and the bean has not been initialized at this time.

2. Call the factory post-processor. Find out the bean of type BeanFactoryPostProcessor from the BeanDefinitionRegistry , and call its postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) method.

3. Register "Bean PostProcessor (BeanPostProcessor)": find beans of type BeanPostProcessor from the BeanDefinitionRegistry , and register these beans in the "Registry of Bean Post Processors"

4. Initialize the message source

5. Initialize the event broadcaster of the application context

6. Initialize other special beans

7. Register event listeners

8. Initialize all single-instance beans except those using lazy mode: after initialization, put the bean in the cache

9. Publish "context refresh events": create "context refresh events", and the event broadcaster is responsible for broadcasting these events to each registered event listener



The complete process of getting a bean from the BeanFactory:

1. Call the getBean(String name) method of beanFactory

2. Call the postProcessBeforeInstantiation () method of InstantiationAwareBeanPostProcessor

3. Instantiate (call the bean's constructor or factory method to instantiate the bean according to the specific configuration)

4. Call the postProcessAfterInstantiation () method of InstantiationAwareBeanPostProcessor

   Here you can do something with the already instantiated bean

5. Call the postProcessPropertyValues () method of InstantiationAwareBeanPostProcessor

6. Set the property value

   If the bean's properties are configured in the bean's configuration file, then perform this action here:

   Specifically, the property setting method of the bean will be called to set the property.

7. Call the setBeanName() method of BeanNameAware

  If the current bean implements the BeanNameAware interface, the setBeanName method will be called to set the name of the bean in the configuration file to the bean

8. Call the setBeanFactory() method of BeanFactoryAware

9. Call the Object postProcessBeforeInitialization (Object bean, String beanName) method of BeanPostProcessor

   BeanPostProcessor can be used to implement AOP, dynamic proxy and other functions

10. Call the afterPropertiesSet() method of InitializingBean

11. Call the initialization method of the init-method attribute configuration

12. Call the postProcessAfterInitialization () method of BeanPostProcessor

Here we need to judge whether it is a singleton or a prototype

If it is a singleton, it is put into spring's singleton cache pool; if it is a prototype, the bean is directly handed over to the caller

When the container is destroyed, no processing is required for the prototype bean;

For singleton beans:

13. Call DisposableBean's afterProperitesSet() method

14. Call the destroy method configured by the destroy-method attribute


The complete process of obtaining beans in ApplicationContext:

Same process as above, just add between 8 and 9 steps

Call the setApplicationContext() method of ApplicationContextAware











Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325493077&siteId=291194637