Springboot2.6.x的啟動(dòng)流程與自動(dòng)配置詳解
一、Springboot啟動(dòng)流程
所有的SpringBoot工程,都有自己的啟動(dòng)類,這個(gè)啟動(dòng)類身上有一個(gè)固定注解@SpringBootApplication,并攜帶一個(gè)main(),通過這個(gè)方法即可完成啟動(dòng)。
啟動(dòng)流程,可以分為2個(gè)步驟:
1、初始化SpringApplication對象階段 (加載相關(guān)資源,判斷是否是個(gè)WEB工程,創(chuàng)建一個(gè)構(gòu)造器(完成自動(dòng)化配置),創(chuàng)建一個(gè)ContextLoadLister,加載主啟動(dòng)類所需要所有組件)
2、run啟動(dòng)應(yīng)用階段
? 開啟各種容器:tomcat容器,springIOC容器(DispatchServlet,ContextLoaderListener),向各種容器加載組件,并配置容器之間上下文環(huán)境
1. 第一步對SpringApplication的初始化
Springboot啟動(dòng)類入口程序,SpringApplication.run方法,先看run方法,再看@SpringBootApplication注解實(shí)現(xiàn)的自動(dòng)配置功能。

run方法點(diǎn)擊后,進(jìn)入源碼,如下圖所示,會(huì)發(fā)現(xiàn)最終第二步源碼中,實(shí)例化了SpringApplication,同時(shí)也運(yùn)行了run方法。

先看SpringApplication的構(gòu)造方法:它干了什么事情!

代碼如下:
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
// resourceLoader 屬性注入了 null
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
// 將啟動(dòng)類從數(shù)組重新封裝成了 Set,注入到 primarySources 屬性
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 得到 web應(yīng)用的類型,這里是 SERVLET
/*
webApplicationType 有三種類型,REACTIVE、SERVLET、NONE
引入 spring-boot-starter-web 包,就是 SERVLET
引入 spring-boot-starter-webflux 包,是 REACTIVE
都沒有就是 NONE
*/
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//從 META-INF/spring.factories
//文件中得到 key 為 org.springframework.boot.BootstrapRegistryInitializer 的全類名集合,進(jìn)行實(shí)例化,然后注入 bootstrapRegistryInitializers 屬性
//其中核心方法getSpringFactoriesInstances,等會(huì)詳細(xì)講解
this.bootstrapRegistryInitializers = new ArrayList<>(
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
//這一行代碼,只是封裝了一下,仍然還是調(diào)用 getSpringFactoriesInstances 方法,
//從 META-INF/spring.factories 文件中得到 key 為org.springframework.context.ApplicationContextInitializer 的全類名集合,
//進(jìn)行實(shí)例化,然后注入 initializers(初始化器集合) 屬性。
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 同理,得到監(jiān)聽器實(shí)例的集合,并注入 簡單解釋getSpringFactoriesInstances就是去spring.factories中讀取配置文件
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 獲取當(dāng)前運(yùn)行的 main 方法所在的類,也就是咱們的主類
this.mainApplicationClass = deduceMainApplicationClass();
}
上述的META-INF/spring.factories 文件,都是在spring-boot.jar包中。
完成了第一步對SpringApplication的初始化模塊,配置一些基本的環(huán)境變量、資源、構(gòu)造器、監(jiān)聽器
提示:第一步操作,getSpringFactoriesInstances方法中,有一個(gè)核心實(shí)例化類是SpringFactoriesLoader類和createSpringFactoriesInstances方法:具體內(nèi)容后面講解。

2. 第二步SpringApplication具體的啟動(dòng)方案
完成SpringApplication的初始化以后,將繼續(xù)運(yùn)行run方法,對啟動(dòng)流程的監(jiān)聽模塊、加載配置環(huán)境模塊、及核心的創(chuàng)建上下文環(huán)境模塊進(jìn)行加載。
回到構(gòu)造方法下,進(jìn)行第二步代碼跟進(jìn):

run方法代碼有點(diǎn)長,截取重點(diǎn)解釋:忽略了所有的catch方法。
/**
運(yùn)行 Spring 應(yīng)用程序,創(chuàng)建并刷新一個(gè)新的ApplicationContext 。
參數(shù):
args – 應(yīng)用程序參數(shù)(通常從 Java 主方法傳遞)
回報(bào):
正在運(yùn)行的ApplicationContext
**/
public ConfigurableApplicationContext run(String... args) {
long startTime = System.nanoTime();
// 添加了一個(gè)默認(rèn)的 Bootstrap 上下文,
//查看createBootstrapContext代碼,就是 new 了一個(gè) DefaultBootstrapContext 實(shí)例,然后遍歷初始化了 bootstrapRegistryInitializers 中的所有初始化器
//bootstrapRegistryInitializers在第一步講解的屬性,實(shí)例化 SpringApplication 時(shí)通過 getSpringFactoriesInstances 方法獲得并注入的。
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
// 配置Headless屬性 無頭模式,不用了解
//此處調(diào)用的是:java.awt.headless
//不提供外部設(shè)備的情況,自行運(yùn)算
configureHeadlessProperty();
// 獲得 RunListener 集合類 這里我們又看到了熟悉的 getSpringFactoriesInstances,
//這次的 key 是 org.springframework.boot.SpringApplicationRunListener 這里會(huì)得到 EventPublishingRunListener 對象
SpringApplicationRunListeners listeners = getRunListeners(args);
// 循環(huán)啟動(dòng)這些監(jiān)聽器 通過廣播模式 得到應(yīng)用監(jiān)聽器后,循環(huán)調(diào)用監(jiān)聽器的 onApplicationEvent方法,具體源碼先跳過
//如果要跟源碼 請參考:starting-->listener.starting(bootstrapContext) -->initialMulticaster.multicastEvent -->invokeListener(listener, event) -->doInvokeListener--->listener.onApplicationEvent(event) 結(jié)束
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
// 封裝參數(shù)
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 創(chuàng)建并配置環(huán)境 第一步讀取并實(shí)例化的3個(gè)配置:
//點(diǎn)擊prepareEnvironment方法進(jìn)入源碼:prepareEnvironment-->1.getOrCreateEnvironment(配置web環(huán)境)-->同級
//2.listeners.environmentPrepared(bootstrapContext, environment)比較重要,下方截圖講解:它包含了環(huán)境配置的處理以及yml文件的讀取
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
//配置yml中配置的需忽略的bean的環(huán)境信息
configureIgnoreBeanInfo(environment);
打印 Banner 就是啟動(dòng)的時(shí)候那個(gè)Springboot圖標(biāo)
Banner printedBanner = printBanner(environment);
// 實(shí)例化上下文對象,因?yàn)轭愋褪?SERVLET,所以實(shí)例化的是 AnnotationConfigServletWebServerApplicationContext 對象
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
// 準(zhǔn)備上下文,以及實(shí)例化bean對象
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
// 刷新上下文:進(jìn)refreshContext進(jìn)入,主要邏輯在 AbstractApplicationContext 對象的 refresh 方法中。
//該方法中有一個(gè)方法是onRefresh(),它內(nèi)部就是創(chuàng)建tomcat的:核心方法是ServletWebServerApplicationContext.createWebServer方法,該方法中的this.webServer = factory.getWebServer(getSelfInitializer());
//getWebServer內(nèi)部實(shí)現(xiàn)類TomcatServletWebServerFactory將會(huì)創(chuàng)建tomcat!
//但是還沒有啟動(dòng)tomcat;AbstractApplicationContext.refresh最后一個(gè)方法finishRefresh();中將會(huì)啟動(dòng)tomcat!
// 請參考下方單獨(dú)提出的tomcat啟動(dòng)相關(guān)
refreshContext(context);
//空方法 啥都沒有
afterRefresh(context, applicationArguments);
// 計(jì)算耗時(shí)
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
//日志打印 不用看
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
}
// 監(jiān)聽器執(zhí)行 started 方法,表示啟動(dòng)成功
listeners.started(context, timeTakenToStartup);
// 回調(diào)所有的ApplicationRunner和CommandLineRunner
callRunners(context, applicationArguments);
}
try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
// 監(jiān)聽器執(zhí)行 ready 方法
listeners.ready(context, timeTakenToReady);
}
return context;
}
重點(diǎn)還是 refreshContext(context);方法:單獨(dú)提出來。
3、refreshContext:核心啟動(dòng)tomcat流程
主要邏輯在 AbstractApplicationContext 對象的 refresh 方法中:上文有一定的描述。具體截圖如下,忽略其他業(yè)務(wù)情況下。

找到核心的抽象類實(shí)現(xiàn):ServletWebServerApplicationContext

createWebServer方法下,將創(chuàng)建tomcat。

繼續(xù)往下,點(diǎn)擊getWebServer方法:

找到tomcat服務(wù)工廠:

tomcat被new出來了,此時(shí)回到入口處:

入口處:

繼續(xù)往下:

進(jìn)入實(shí)現(xiàn)類DefaultLifecycleProcessor中,通過startBeans繼續(xù)往下:

繼續(xù)往下:

看見do開頭的方法了,已經(jīng)到了真正代碼實(shí)現(xiàn)的地方了,繼續(xù)往下

繼續(xù)進(jìn)入doStart方法:

進(jìn)入start方法:

進(jìn)入WebServerStartStopLifecycle的start實(shí)現(xiàn)中:

再次繼續(xù)進(jìn)入:

在web服務(wù)啟動(dòng)中,找到Tomcat的啟動(dòng)實(shí)現(xiàn):

最終完成啟動(dòng),看見了Tomcat的啟動(dòng)過程,不過volatile不懂的,這個(gè)地方也不好解釋,請自行研究JMM:

## 3. 總結(jié)
1)實(shí)例化 SpringApplication 對象
2)通過spring.factories 得到 初始化BootstrapRegistryInitializer、ApplicationContextInitializer 和 監(jiān)聽器ApplicationListener
3)調(diào)用 run 方法
4)記錄開始時(shí)間
5)得到 runListeners
6)runListeners 執(zhí)行 starting
7)準(zhǔn)備環(huán)境
8)打印 banner
9)實(shí)例化上下文對象
10)準(zhǔn)備上下文,執(zhí)行之前得到的初始化器的初始化方法,load主bean
11)刷新上下文,在其中加載 autoConfiguration,并啟動(dòng) Tomcat
12)計(jì)算耗時(shí)
13)打印耗時(shí)
14)通知監(jiān)聽器啟動(dòng)完成
15)通知監(jiān)聽器 ready
4. 補(bǔ)充 getSpringFactoriesInstances
在第一步SpringApplication實(shí)例化的時(shí)候:3個(gè)getSpringFactoriesInstances方法的詳細(xì)解析!

隨便點(diǎn)擊一個(gè)進(jìn)入:

這里面比較關(guān)鍵的邏輯是 得到類的全類名集合 和 實(shí)例化類,第一個(gè)紅框下
SpringFactoriesLoader.loadFactoryNames(type, classLoader)該方法下:

loadSpringFactories方法中:

從這些代碼我們可以得知,會(huì)從 spring-boot.jar下的META-INF/spring.factories 文件中找到 key 匹配的類,并把類的全路徑集合得到;

回到之前第二個(gè)紅框下:如下圖

例如實(shí)例化 SpringApplication 對象時(shí),獲得 初始化器 和 監(jiān)聽器:

之后通過全類名,使用反射技術(shù),實(shí)例化類,最終得到想要的集合
二、Springboot自動(dòng)配置原理
1. @SpringBootApplication
該注解為啟動(dòng)注解,注解結(jié)構(gòu)是:

作用:
1、完成容器的自動(dòng)掃描 @ComponentScan
2、完成自動(dòng)配置@EnableAutoConfiguration
3、加載配置類@SpringBootConfiguration
頁面:不推薦使用JSP,相反:Freemarker,Thymeleaf這頁面技術(shù)(模板引擎:JSTL)
2自動(dòng)配置流程
1)、SpringBoot啟動(dòng)的時(shí)候加載主配置類,開啟了自動(dòng)配置功能 @EnableAutoConfiguration

2)、@EnableAutoConfiguration 作用:
利用EnableAutoConfigurationImportSelector給容器中導(dǎo)入一些組件?

可以查看selectImports()方法的內(nèi)容,其中核心代碼getAutoConfigurationEntry;

在AutoConfigurationImportSelector的getAutoConfigurationEntry方法中:List configurations = getCandidateConfigurations(annotationMetadata, attributes);獲取候選的配置:debug截圖!

在getCandidateConfigurations方法中,SpringFactoriesLoader.loadFactoryNames()核心方法:

繼續(xù)進(jìn)入loadFactoryNames()方法,這個(gè)方法會(huì)掃描所有jar包類路徑下 META-INF/spring.factories:

進(jìn)入loadSpringFactories方法中,獲取到常量配置META-INF/spring.factories:

上圖所示:把掃描到的這些文件的內(nèi)容包裝成properties對象,從properties中獲取到key為EnableAutoConfiguration.class類(全限定類名)對應(yīng)的值,然后把他們添加在容器中。
將類路徑下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;
可以去依賴包中,spring-boot-autoconfigure中,找到META-INF下的spring.factories,部分代碼如下:
# 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.context.ConfigurationPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.context.LifecycleAutoConfiguration,\ 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.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration,\ 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.Neo4jReactiveDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jReactiveRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration,\ 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.ElasticsearchRestClientAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration,\ 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.jersey.JerseyAutoConfiguration,\ org.springframework.boot.autoconfigure.jooq.JooqAutoConfiguration,\ org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration,\ org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration,\ org.springframework.boot.autoconfigure.availability.ApplicationAvailabilityAutoConfiguration,\ 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.neo4j.Neo4jAutoConfiguration,\ org.springframework.boot.autoconfigure.netty.NettyAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration,\ org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration,\ org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketMessagingAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketRequesterAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration,\ org.springframework.boot.autoconfigure.rsocket.RSocketStrategiesAutoConfiguration,\ org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\ 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.security.rsocket.RSocketSecurityAutoConfiguration,\ org.springframework.boot.autoconfigure.security.saml2.Saml2RelyingPartyAutoConfiguration,\ 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.sql.init.SqlInitializationAutoConfiguration,\ 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.ReactiveMultipartAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.WebSessionIdResolverAutoConfiguration,\ 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
每一個(gè)這樣的 xxxAutoConfiguration類都是容器中的一個(gè)組件,都加入到容器中;用他們來做自動(dòng)配置;
3)、每一個(gè)自動(dòng)配置類進(jìn)行自動(dòng)配置功能;
4)、以**HttpEncodingAutoConfiguration(Http編碼自動(dòng)配置)**為例解釋自動(dòng)配置原理;
@Configuration(proxyBeanMethods = false) //表示這是一個(gè)配置類,以前編寫的配置文件一樣,也可以給容器中添加組件
@EnableConfigurationProperties(ServerProperties.class) //啟動(dòng)指定類的ConfigurationProperties功能;將配置文件中對應(yīng)的值和HttpEncodingProperties綁定起來;并把HttpEncodingProperties加入到ioc容器中
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)//Spring底層@Conditional注解(Spring注解版),根據(jù)不同的條件,如果滿足指定的條件,整個(gè)配置類里面的配置就會(huì)生效; 判斷當(dāng)前應(yīng)用是否是web應(yīng)用,如果是,當(dāng)前配置類生效
@ConditionalOnClass(CharacterEncodingFilter.class)//判斷當(dāng)前項(xiàng)目有沒有這個(gè)類CharacterEncodingFilter;SpringMVC中進(jìn)行亂碼解決的過濾器;
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)
//判斷配置文件中是否存在某個(gè)配置 spring.http.encoding.enabled;如果不存在,判斷也是成立的
//即使我們配置文件中不配置pring.http.encoding.enabled=true,也是默認(rèn)生效的;
public class HttpEncodingAutoConfiguration {
//他已經(jīng)和SpringBoot的配置文件映射了
private final Encoding properties;
//只有一個(gè)有參構(gòu)造器的情況下,參數(shù)的值就會(huì)從容器中拿
public HttpEncodingAutoConfiguration(ServerProperties properties) {
this.properties = properties.getServlet().getEncoding();
}
@Bean //給容器中添加一個(gè)組件,這個(gè)組件的某些值需要從properties中獲取
@ConditionalOnMissingBean //判斷容器沒有這個(gè)組件?
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Encoding.Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Encoding.Type.RESPONSE));
return filter;
}
根據(jù)當(dāng)前不同的條件判斷,決定這個(gè)配置類是否生效?
一但這個(gè)配置類生效;這個(gè)配置類就會(huì)給容器中添加各種組件;這些組件的屬性是從對應(yīng)的properties類中獲取的,這些類里面的每一個(gè)屬性又是和配置文件綁定的;
5)、所有在配置文件中能配置的屬性都是在xxxxProperties類中封裝者‘;配置文件能配置什么就可以參照某個(gè)功能對應(yīng)的這個(gè)屬性類
@ConfigurationProperties(prefix = "spring.http.encoding") //從配置文件中獲取指定的值和bean的屬性進(jìn)行綁定
public class HttpEncodingProperties {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
精髓:
? 1)、SpringBoot啟動(dòng)會(huì)加載大量的自動(dòng)配置類
? 2)、我們看我們需要的功能有沒有SpringBoot默認(rèn)寫好的自動(dòng)配置類;
? 3)、我們再來看這個(gè)自動(dòng)配置類中到底配置了哪些組件;(只要我們要用的組件有,我們就不需要再來配置了)
? 4)、給容器中自動(dòng)配置類添加組件的時(shí)候,會(huì)從properties類中獲取某些屬性。我們就可以在配置文件中指定這些屬性的值;
xxxxAutoConfigurartion:自動(dòng)配置類;
給容器中添加組件
xxxxProperties:封裝配置文件中相關(guān)屬性;
3、額外注解學(xué)習(xí)
1、@Conditional派生注解(Spring注解版原生的@Conditional作用)
作用:必須是@Conditional指定的條件成立,才給容器中添加組件,配置配里面的所有內(nèi)容才生效;
| @Conditional擴(kuò)展注解 | 作用(判斷是否滿足當(dāng)前指定條件) |
|---|---|
| @ConditionalOnJava | 系統(tǒng)的java版本是否符合要求 |
| @ConditionalOnBean | 容器中存在指定Bean; |
| @ConditionalOnMissingBean | 容器中不存在指定Bean; |
| @ConditionalOnExpression | 滿足SpEL表達(dá)式指定 |
| @ConditionalOnClass | 系統(tǒng)中有指定的類 |
| @ConditionalOnMissingClass | 系統(tǒng)中沒有指定的類 |
| @ConditionalOnSingleCandidate | 容器中只有一個(gè)指定的Bean,或者這個(gè)Bean是首選Bean |
| @ConditionalOnProperty | 系統(tǒng)中指定的屬性是否有指定的值 |
| @ConditionalOnResource | 類路徑下是否存在指定資源文件 |
| @ConditionalOnWebApplication | 當(dāng)前是web環(huán)境 |
| @ConditionalOnNotWebApplication | 當(dāng)前不是web環(huán)境 |
| @ConditionalOnJndi | JNDI存在指定項(xiàng) |
自動(dòng)配置類必須在一定的條件下才能生效;
我們怎么知道哪些自動(dòng)配置類生效;
我們可以通過啟用 debug=true屬性;來讓控制臺(tái)打印自動(dòng)配置報(bào)告,這樣我們就可以很方便的知道哪些自動(dòng)配置類生效;
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:(自動(dòng)配置類啟用的)
-----------------
DispatcherServletAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
- @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)
Negative matches:(沒有啟動(dòng),沒有匹配成功的自動(dòng)配置類)
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)
AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)
總結(jié)
到此這篇關(guān)于Springboot2.6.x啟動(dòng)流程與自動(dòng)配置的文章就介紹到這了,更多相關(guān)Springboot2.6.x啟動(dòng)流程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring?Boot實(shí)現(xiàn)第一次啟動(dòng)時(shí)自動(dòng)初始化數(shù)據(jù)庫流程詳解
- SpringBoot熱部署啟動(dòng)關(guān)閉流程詳解
- SpringBoot啟動(dòng)流程SpringApplication準(zhǔn)備階段源碼分析
- SpringBoot自定義啟動(dòng)器Starter流程詳解
- SpringBoot超詳細(xì)分析啟動(dòng)流程
- Spring?Boot面試必問之啟動(dòng)流程知識(shí)點(diǎn)詳解
- springboot中swagger快速啟動(dòng)流程
- SpringBoot中如何啟動(dòng)Tomcat流程
- Spring Boot 啟動(dòng)流程解析
相關(guān)文章
Java中BigInteger與BigDecimal類用法總結(jié)
在Java中有兩個(gè)用于大數(shù)字運(yùn)算的類,分別是java.math.BigInteger類 和 java.math.BigDecimal類,這兩個(gè)類都可以用于高精度計(jì)算,BigInteger類是針對整型大數(shù)字的處理類,而BigDecimal類是針對大小數(shù)的處理類,接下來帶大家來學(xué)習(xí)一下,在Java中如何處理大數(shù)字2023-05-05
Java中生成隨機(jī)數(shù)的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Java中生成隨機(jī)數(shù)的實(shí)現(xiàn)方法總結(jié),其中多線程并發(fā)的實(shí)現(xiàn)方式尤為exciting,需要的朋友可以參考下2015-11-11
Springboot?通過FastJson實(shí)現(xiàn)bean對象和Json字符串互轉(zhuǎn)問題
這篇文章主要介紹了Springboot?通過FastJson實(shí)現(xiàn)bean對象和Json字符串互轉(zhuǎn),本文嘗試驗(yàn)證兩種場景給大家詳細(xì)介紹,對Springboot?FastJson實(shí)現(xiàn)bean和Json互轉(zhuǎn)問題,感興趣的朋友一起看看吧2022-08-08
基于Java解決華為機(jī)試實(shí)現(xiàn)密碼截取?
這篇文章主要介紹了基于Java解決華為機(jī)試實(shí)現(xiàn)密碼截取,文章圍繞主題相關(guān)資料展開詳細(xì)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下,希望對你有所幫助2022-02-02
SpringMVC返回的ResponseEntity出現(xiàn)亂碼及解決
這篇文章主要介紹了SpringMVC返回的ResponseEntity出現(xiàn)亂碼及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
springboot接口參數(shù)校驗(yàn)JSR303的實(shí)現(xiàn)
本文主要介紹了springboot接口參數(shù)校驗(yàn)JSR303的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
淺析java修飾符訪問權(quán)限(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
Java有四種訪問權(quán)限,其中三種有訪問權(quán)限修飾符,分別為private,public和protected,還有一種不帶任何修飾符,下面通過本文給大家簡單介紹下java修飾符訪問權(quán)限相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2017-04-04

