Spring source code analysis (6) startup process analysis

Check out my other Spring source code analysis articles

https://blog.csdn.net/caicongyang/category_2139981.html?spm=1001.2014.3001.5482

1. this() method

a.构建DefaultListableBeanFactory、AnnotatedBeanDefinitionReader、ClassPathBeanDefinitionScanner

b. Register the relevant BeanFactoryProcess with BeanDefinitionRegistry in advance during the construction of AnnotatedBeanDefinitionReader.

源码见org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry, java.lang.Object)

Register BeanDefinition here in advance:

ConfigurationClassPostProcessor
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
internalPersistenceAnnotationProcessor
DefaultEventListenerFactory
EventListenerMethodProcessor

2.register(componentClasses) method

Register the configuration class with BeanDefinitionRegistry

3.refresh() method

a.prepareRefresh(): Start time, etc., set configuration information to Environment

b.obtainFreshBeanFactory(): Determine whether to refresh the container balance

c.prepareBeanFactory(beanFactory):

After mentioning, add two beans, ApplicationContextAwareProcessor/ApplicationListenerDetector, to the beanfactory.

Add resolvableDependencies: When performing dependency injection byType, the bean will be found based on the type from this attribute.

Simply understand it as injecting this bean in advance;

	// BeanFactory interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
		beanFactory.registerResolvableDependency(ApplicationContext.class, this);

d.invokeBeanFactoryPostProcessors(beanFactory) 方法
​​​​​​

Execute BeanFactoryPostProcessor. The focus is on the beanFactoryPostProcessor that was put in earlier. The beanFactoryPostProcessor recursively parses the @Component @Bean @Import @ImportSource class and scans and produces all BeanDefinitions; 

The core classes are:

org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions

e.registerBeanPostProcessors(beanFactory)方法

Instantiate and sort the scanned BeanPostProcessors, and add them to the beanPostProcessors property of BeanFactory

f.initMessageSource() method

Set the MessageSource of ApplicationContext,

g.initApplicationEventMulticaster();

publish broadcast

h.registerListeners();

Register listener

i.finishBeanFactoryInitialization(beanFactory);

Initialize singleton method

j.finishRefresh();

Publish some life cycle events, etc.

 
 

Guess you like

Origin blog.csdn.net/caicongyang/article/details/122897263