springboot启动流程梳理

启动顺序主要针对SpringApplication.run()方法的梳理

一 SpringApplication类的实例化

  1. BootstrapRegistryInitializer接口实例化
    1).加载 BootstrapRegistryInitializer实现类 ,由 SpringFactoriesLoader 类加载定义在 META-INF/spring.factories 文件夹下的配置文件Resource 初始化资源列表 Map<ClassLoader, MultiValueMap<String, String>>
    2).加载实例化BootstrapRegistryInitializer的实现类

  2. ApplicationContextInitializer 实现类的资源配置文件读取以及实现相关类的实例化
    1).加载 ApplicationContextInitializer 实现类 ,由 SpringFactoriesLoader 类加载定义在 META-INF/spring.factories 文件夹下的配置文件Resource 初始化资源列表 Map<ClassLoader, MultiValueMap<String, String>>
    2).加载实例化ApplicationContextInitializer 的实现类 ,springboot启动内置的实例化类:

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
  1. ApplicationListener 接口类的资源配置文件的读取以及实例化
    1).加载 ApplicationListener 实现类 ,由 SpringFactoriesLoader 类加载定义在 META-INF/spring.factories 文件夹下的配置文件Resource 初始化资源列表 Map<ClassLoader, MultiValueMap<String, String>>
    2) ApplicationListener 的实现类,springboot内置的实例化类
org.springframework.boot.autoconfigure.BackgroundPreinitializer

二 SpringApplication.run()启动方法

  1. this.createBootstrapContext(),执行BootstrapRegistryInitializer接口的实例化类调用执行。

  2. SpringApplicationRunListeners的实例化以及相关实现类的调用
    1).实例化 SpringApplicationRunListeners,启动 SpringApplicationRunListeners的监听
    由 SpringFactoriesLoader 类加载 SpringApplicationRunListeners 实现类 ,定义在 META-INF/spring.factories 文件夹下的配置文件Resource 初始化资源列表 Map<ClassLoader, MultiValueMap<String, String>>
    2).遍历调用所有SpringApplicationRunListeners的starting方法,实现实现类接口的初始化

  3. 实例化 ConfigurableEnvironment ,由listeners,bootstrapContext,applicationArguments设置启动springboot启动的环境配置信息

  4. 打印Banner

  5. 创建ConfigurableApplicationContext类

  6. 创建加载SpringBootExceptionReporter
    1).加载 创建加载SpringBootExceptionReporter 实现类 ,定义在 META-INF/spring.factories 文件夹下的配置文件Resource 初始化资源列表 Map<ClassLoader, MultiValueMap<String, String>>
    2).创建加载SpringBootExceptionReporter 的实现类

  7. 初始化容器prepareContext
    根据初始化的ConfigurableApplicationContext、ConfigurableEnvironment、SpringApplicationRunListeners、ApplicationArguments、Banner 准备初始化上下文context
    1) 设置实现ApplicationContextInitializer 的实现类的context属性
    2) 设置listener的context属性
    3)设置beanFactory 的属性 ,加载bean的时候是否循环依赖allowCircularReferences
    4) 设置beanFactory的属性 ,加载bean 时候是否允许同名bean的覆盖属性allowBeanDefinitionOverriding,springboot默认false
    5) 设置context 属性 lazyInitialization ,配置是否延迟加载懒加载的bean。
    6) 创建加载bean的BeanDefinitionLoader。和定义bean 加载时候的resourceLoader,environment

懒加载的bean配合@Lazy注解使用。懒加载好处:
节省资源:当应用程序中存在大量的 bean 时,立即初始化所有 bean 可能会占用大量的内存和处理时间。通过延迟初始化,只有在需要使用 bean 时才会进行初始化,可以避免不必要的资源消耗。
加快启动时间:延迟初始化可以减少应用程序启动时间,因为只有在需要时才会加载和初始化
bean。对于那些在应用程序启动时可能不会使用的较大或复杂的 bean,延迟初始化可以显著加快启动时间。
解决循环依赖: Spring 容器可以管理bean之间的依赖关系。当存在循环依赖时,延迟初始化可以帮助解决这个问题。通过延迟初始化,Spring
容器可以在运行时逐个解析和满足 bean 之间的依赖,而不是在初始化阶段发现无法解决的循环依赖。

  1. 更新上下文 refreshContext
    1) 通过registerShutdownHook 钩子方法判断容器是进行开启还是关闭
    2) 给beanFactory进行refresh()
    (1) 设置BeanFactory的后置处理器,实现了BeanFactoryPostProcessor接口
    (2)设置bean的后置处理器,实现了BeanPostProcessor接口
    (3) 初始化国际化资源,实现了HierarchicalMessageSource接口
    (4)初始化spring容器实践,实现了ApplicationEventMulticaster接口
    (5)onRefresh() 更新主题资源,实现了HierarchicalThemeSource接口, 创建ServletContext
    (6)注册监监听事件ApplicationListener事件,获取ApplicationListener实现接口集合,执行广播监听(ApplicationEventMulticaster接口实现类),执行对应的excute方法;执行 listener.onApplicationEvent(event)方法;
    (7)加载通过字节码载入字节码文件时动态织入Aspect切面,LoadTimeWeaverAware接口
    (8) 加载实例化LifecycleProcessor接口,调用start/stop相关接口
    在Spring中还提供了Lifecycle接口,Lifecycle接口中包含start/stop方法,实现此接口后Spring会保证在启动的时候调用其start方法开始生命周期,并在Spring关闭的时候调用stop方法来结束生命周期,通常用来配置后台程序,如启动后一直运行(如对MQ进行轮询等)。而ApplicationContext的初始化最后一步就是保证了这一功能的实现。
    (9) 调用ContextRefreshedEvent 事件机制,实现ApplicationContextEvent实现ApplicationEvent 抽象类
  2. 打印启动日志 logStartupInfo
  3. 执行接口事件、ApplicationRunner、CommandLineRunner接口事件集合

springboot 启动流程图

在这里插入图片描述

springboot 创建上下文 createApplicationContext 过程

在这里插入图片描述

spring-boot -autoconfig自动装配模块下META-INF/spring.factories自动装配清单

springboot在启动的时候,启动加载jar包下META-INF/spring.factories文件,然后SpringFactoriesLoader.loadFactoryNames加载实例化实现指定注解,接口的实现类,完成完成对应的自动装配。
例如 springbootApplicatin启动类下的@ EnableAutoConfiguration 注解,加载装配配置类;通过EnableAutoConfiguration 注解类,加载实例化org.springframework.boot.autoconfigure.EnableAutoConfiguration配置清单下的配置类。

SpringFactoriesLoader.loadFactoryNames加载类说明,是根据类型加载类

 loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader)```
# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener

# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudServiceConnectorsAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoReactiveRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,\
org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration,\
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\
org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration,\
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration,\
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.JndiConnectionFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration,\
org.springframework.boot.autoconfigure.jms.artemis.ArtemisAutoConfiguration,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration,\
org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\
org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration,\
org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration,\
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,\
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\
org.springframework.boot.autoconfigure.reactor.core.ReactorCoreAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityRequestMatcherProviderAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration,\
org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration,\
org.springframework.boot.autoconfigure.session.SessionAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration,\
org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration,\
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.ClientHttpConnectorAutoConfiguration,\
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration

# Failure analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.DataSourceBeanCreationFailureAnalyzer,\
org.springframework.boot.autoconfigure.jdbc.HikariDriverConfigurationFailureAnalyzer,\
org.springframework.boot.autoconfigure.session.NonUniqueSessionRepositoryFailureAnalyzer

# Template availability providers
org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider=\
org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.mustache.MustacheTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafTemplateAvailabilityProvider,\
org.springframework.boot.autoconfigure.web.servlet.JspTemplateAvailabilityProvider

springboot 扩展点详解

参考:https://blog.csdn.net/fox9916/article/details/129101918

猜你喜欢

转载自blog.csdn.net/tian830937/article/details/132819632