美文网首页
SpringBoot启动过程

SpringBoot启动过程

作者: 异或 | 来源:发表于2021-11-16 17:06 被阅读0次
  1. SpringApplicaiton初始化
field value
resourceLoader null
primarySources Main.class
webApplicationType NONE
bootstrapRegistryInitializers null
initializers org.springframework.boot.context.config.DelegatingApplicationContextInitializer、org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer、org.springframework.boot.context.ContextIdApplicationContextInitializer、org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer、org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer、org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer、org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
listeners org.springframework.boot.env.EnvironmentPostProcessorApplicationListener、org.springframework.boot.context.config.AnsiOutputApplicationListener、org.springframework.boot.context.logging.LoggingApplicationListener、org.springframework.boot.autoconfigure.BackgroundPreinitializer、org.springframework.boot.context.config.DelegatingApplicationListener、org.springframework.boot.builder.ParentContextCloserApplicationListener、org.springframework.boot.ClearCachesApplicationListener、org.springframework.boot.context.FileEncodingApplicationListener
mainApplicationClass Main.class
  1. SpingApplication启动
    2.1 创建DefaultBootstrapContext
    2.2 bootstrapRegistryInitializers初始化,执行bootstrapRegistryInitializers(empty)
    2.3 SpringApplicationRunListeners初始化,执行listener.starting(bootstrapContext)
    |listeners|org.springframework.boot.context.event.EventPublishingRunListener|
    2.4 初始化environmenttodo
    2.5 创建ApplicaitonContext
field value
reader AnnotatedBeanDefinitionReader
scanner ClassPathBeanDefinitionScanner

2.6 ApplicaitonContext
2.6.1 postProcessApplicationContext

context.getBeanFactory().registerSingleton("org.springframework.context.annotation.internalConfigurationBeanNameGenerator",this.beanNameGenerator);
context.setResourceLoader(this.resourceLoader);
context.getBeanFactory().setConversionService(context.getEnvironment().getConversionService());

2.6.2 applyInitializers -- 遍历执行SpringApplicaiton初始化阶段生成的initializers

Initializer desc
DelegatingApplicationContextInitializer nothing
SharedMetadataReaderFactoryContextInitializer applicationContext.addBeanFactoryPostProcessor(new CachingMetadataReaderFactoryPostProcessor(applicationContext))
ContextIdApplicationContextInitializer applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(), contextId)
ConfigurationWarningsApplicationContextInitializer context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()))
RSocketPortInfoApplicationContextInitializer applicationContext.addApplicationListener(new Listener(applicationContext))
ServerPortInfoApplicationContextInitializer applicationContext.addApplicationListener(this)
ConditionEvaluationReportLoggingListener applicationContext.addApplicationListener(new ConditionEvaluationReportListener())、
beanFactory.registerSingleton("autoConfigurationReport", new ConditionEvaluationReport())

2.6.3 SpringApplicationRunListeners 发送contextPrepared事件
2.6.4 bootstrapContext关闭
2.6.7 beanFactory中注册bean

beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
beanFactory.registerSingleton("springBootBanner", printedBanner);

2.6.8 context中添加beanFactoryPostProcessor (when lazyInitialization = true)

context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());

2.6.9 加载BeanDefinition
2.6.9.1 创建BeanDefinitionLoader

    BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
        Assert.notNull(registry, "Registry must not be null");
        Assert.notEmpty(sources, "Sources must not be empty");
        this.sources = sources;
        this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
        this.xmlReader = (XML_ENABLED ? new XmlBeanDefinitionReader(registry) : null);
        this.groovyReader = (isGroovyPresent() ? new GroovyBeanDefinitionReader(registry) : null);
        this.scanner = new ClassPathBeanDefinitionScanner(registry);
        this.scanner.addExcludeFilter(new ClassExcludeFilter(sources));
    }

2.6.9.2 执行loader,加载启动类

// 解析注解
@scope(value=singleton,proxyMode=ScopedProxyMode.NO)
@Lazy
@Primary
@DependsOn
@Role
@Description
// 注入BeanDefinition
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition())

2.6.10 SpringApplicationRunListeners 发送contextLoaded事件
2.7 refreshApplicationContext
2.7.1 invokeBeanFactoryPostProcessors 执行beanFactoryProcessor
2.7.1.1 执行2.6.2 applyInitializers阶段中注入的beanFactoryProcessor

beanFactoryPostProcessor desc
CachingMetadataReaderFactoryPostProcessor registry.registerBeanDefinition("org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory", SharedMetadataReaderFactoryBean);
ConfigurationWarningsApplicationContextInitializer warn

2.7.1.2 获取beanFactory中类型为BeanDefinitionRegistryPostProcessor的processor, 得到ConfigurationClassPostProcessor并执行
2.7.1.2.1 处理启动类

// 解析@Compment标签
if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
    processMemberClasses(configClass, sourceClass, filter)
}
// 解析@PropertySource @PropertySources标签
for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
                sourceClass.getMetadata(), PropertySources.class,
                org.springframework.context.annotation.PropertySource.class)) {
            if (this.environment instanceof ConfigurableEnvironment) {
                processPropertySource(propertySource);
}
// 解析 @ComponentScan @ComponentScans标签,并递归scan该包下类及@Import注解,生成beanDefinitionHolder
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
                sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
Set<BeanDefinitionHolder> scannedBeanDefinitions =
                        this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName())
// 

2.7.1.3

  • 执行剩余PostProcessor:SharedMetadataReaderFactoryContextInitializer$CachingMetadataReaderFactoryPostProcessor
    ConfigurationWarningsApplicationContextInitializer$ConfigurationWarningsPostProcessor
    ConfigurationClassPostProcessor

2.7.1.4 其他PostProcessor

  • 执行priorityOrderedPostProcessor:PropertySourcesPlaceholderConfigurer
  • 执行orderedPostProcessor:DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor
  • 执行nonOrderedPostProcessor:EventListenerMethodProcessor

相关文章

网友评论

      本文标题:SpringBoot启动过程

      本文链接:https://www.haomeiwen.com/subject/acqhtrtx.html