Springboot startup process combing

The startup sequence is mainly aimed at sorting out the SpringApplication.run() method

An instantiation of the SpringApplication class

  1. BootstrapRegistryInitializer interface instantiation
    1). Load the BootstrapRegistryInitializer implementation class, and the SpringFactoriesLoader class loads the configuration file Resource defined in the META-INF/spring.factories folder to initialize the resource list Map<ClassLoader, MultiValueMap<String, String>> 2).
    Load Instantiate the implementation class of BootstrapRegistryInitializer

  2. ApplicationContextInitializer reads the resource configuration file of the implementation class and instantiates the related classes
    1). Load the ApplicationContextInitializer implementation class, and the SpringFactoriesLoader class loads the configuration file defined in the META-INF/spring.factories folder. Resource initializes the resource list Map<ClassLoader , MultiValueMap<String, String>>
    2). Load the implementation class that instantiates ApplicationContextInitializer, and springboot starts the built-in instantiation class:

org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
  1. Reading and instantiation of the resource configuration file of the ApplicationListener interface class
    1). Load the ApplicationListener implementation class, and the SpringFactoriesLoader class loads the configuration file defined in the META-INF/spring.factories folder. Resource initializes the resource list Map<ClassLoader, MultiValueMap< String, String>>
    2) Implementation class of ApplicationListener, springboot’s built-in instantiation class
org.springframework.boot.autoconfigure.BackgroundPreinitializer

2. SpringApplication.run() startup method

  1. this.createBootstrapContext(), executes the instantiation class call of the BootstrapRegistryInitializer interface.

  2. Instantiation of SpringApplicationRunListeners and invocation of related implementation classes
    1). Instantiate SpringApplicationRunListeners and start the monitoring of SpringApplicationRunListeners. The SpringApplicationRunListeners
    implementation class is loaded by the SpringFactoriesLoader class and is defined in the configuration file Resource in the META-INF/spring.factories folder. Initialize the resource list Map< ClassLoader, MultiValueMap<String, String>>
    2). Traverse and call the starting method of all SpringApplicationRunListeners to implement the initialization of the class interface

  3. Instantiate ConfigurableEnvironment, and set the environment configuration information for springboot startup by listeners, bootstrapContext, and applicationArguments.

  4. Print Banner

  5. Create ConfigurableApplicationContext class

  6. Create and load SpringBootExceptionReporter
    1). Load Create and load SpringBootExceptionReporter implementation class, defined in the configuration file Resource in the META-INF/spring.factories folder. Initialize resource list Map<ClassLoader, MultiValueMap<String, String>> 2). Create and load
    SpringBootExceptionReporter Implementation class

  7. Initialize the container prepareContext
    to prepare the initialized context context based on the initialized ConfigurableApplicationContext, ConfigurableEnvironment, SpringApplicationRunListeners, ApplicationArguments, and Banner 1
    ) Set the context attribute of the implementation class that implements ApplicationContextInitializer
    2) Set the context attribute of the listener
    3) Set the beanFactory attribute to determine whether to loop when loading the bean Depends on allowCircularReferences
    4) Set the properties of beanFactory, whether to allow the overriding attribute allowBeanDefinitionOverriding of beans with the same name when loading beans, springboot defaults to false
    5) Set the context attribute lazyInitialization, configure whether to delay loading of lazy-loaded beans.
    6) Create a BeanDefinitionLoader that loads beans. And define the resourceLoader and environment when loading the bean

Lazy loaded beans are used with the @Lazy annotation. Benefits of lazy loading:
Saving resources: When there are a large number of beans in an application, initializing all beans at once may take up a lot of memory and processing time. Through lazy initialization, the bean will be initialized only when it is needed, which can avoid unnecessary resource consumption.
Faster startup time: Lazy initialization can reduce application startup time because
beans are loaded and initialized only when needed. For larger or complex beans that may not be used when the application starts, lazy initialization can significantly speed up startup time.
Resolve circular dependencies: Spring container can manage dependencies between beans. When there are circular dependencies, lazy initialization can help solve the problem. Through lazy initialization, the Spring
container can resolve and satisfy dependencies between beans one by one at runtime, instead of discovering unresolved cyclic dependencies during the initialization phase.

  1. Update context refreshContext
    1) Determine whether the container is opened or closed through the registerShutdownHook hook method
    2) Refresh() the beanFactory
    (1) Set the post-processor of the BeanFactory and implement the BeanFactoryPostProcessor interface
    (2) Set the post-processor of the bean, Implemented the BeanPostProcessor interface
    (3) to initialize international resources, implemented the HierarchicalMessageSource interface
    (4) initialized the spring container practice, implemented the ApplicationEventMulticaster interface
    (5) onRefresh() updated the theme resources, implemented the HierarchicalThemeSource interface, created the ServletContext
    (6) registered the monitor Listen to the ApplicationListener event, obtain the ApplicationListener implementation interface collection, perform broadcast monitoring (ApplicationEventMulticaster interface implementation class), and execute the corresponding excute method; execute the listener.onApplicationEvent(event) method; (7) Load the
    bytecode file through bytecode When dynamically weaving Aspect aspects, the LoadTimeWeaverAware interface
    (8) loads the instantiated LifecycleProcessor interface and calls the start/stop related interfaces.
    Spring also provides the Lifecycle interface, which contains the start/stop method. After implementing this interface, Spring will ensure that its start method is called to start the life cycle when it is started, and the stop method is called when Spring is shut down to end the life cycle. , usually used to configure the background program, such as running continuously after startup (such as polling MQ, etc.). The last step of ApplicationContext initialization is to ensure the realization of this function.
    (9) Call the ContextRefreshedEvent event mechanism and implement ApplicationContextEvent to implement the ApplicationEvent abstract class
  2. Print startup log logStartupInfo
  3. Execution interface events, ApplicationRunner, CommandLineRunner interface event collection

springboot startup flow chart

Insert image description here

springboot creates context createApplicationContext process

Insert image description here

META-INF/spring.factories automatic assembly list under spring-boot -autoconfig automatic assembly module

When springboot starts, it starts loading the META-INF/spring.factories file under the jar package, and then SpringFactoriesLoader.loadFactoryNames loads the instantiation implementation of the specified annotation and the implementation class of the interface to complete the corresponding automatic assembly.
For example, the @EnableAutoConfiguration annotation under the springbootApplicatin startup class loads the assembly configuration class; through the EnableAutoConfiguration annotation class, the configuration class under the instantiated org.springframework.boot.autoconfigure.EnableAutoConfiguration configuration list is loaded.

SpringFactoriesLoader.loadFactoryNames loading class description is to load the class according to the type

 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

Detailed explanation of springboot extension points

Reference: https://blog.csdn.net/fox9916/article/details/129101918

Guess you like

Origin blog.csdn.net/tian830937/article/details/132819632