Spring Boot啟動(dòng)流程分析
引言
早在15年的時(shí)候就開始用spring boot進(jìn)行開發(fā)了,然而一直就只是用用,并沒有深入去了解spring boot是以什么原理怎樣工作的,說(shuō)來(lái)也慚愧。今天讓我們從spring boot啟動(dòng)開始,深入了解一下spring boot的工作原理。
為什么用spring boot
在使用一個(gè)東西或者一個(gè)工具之前,我們總是會(huì)問自己,我為什么要用?用他能給我?guī)?lái)什么好處?
* 最大的好處就是spring boot遵從了java**約定大于配置**不用面對(duì)一大堆的配置文件,spring boot是根據(jù)你用的包來(lái)決定提供什么配置。
* 服務(wù)器以jar包的形式內(nèi)嵌于項(xiàng)目中,對(duì)于微服務(wù)滿天飛的情況,spring boot天生適合微服務(wù)架構(gòu),方便部署。
* 提供devtools從此改代碼就需重啟成為歷史。
有優(yōu)點(diǎn)就一定有缺點(diǎn),缺點(diǎn)來(lái)源于優(yōu)點(diǎn)優(yōu)點(diǎn)來(lái)源于缺點(diǎn)(感覺在說(shuō)哲學(xué)問題了哈哈哈)
* 正因?yàn)榕渲脤?duì)開發(fā)者不透明,不看源碼會(huì)不清楚spring boot如何進(jìn)行諸如JDBC加載、事務(wù)管理等,出現(xiàn)錯(cuò)誤也很難調(diào)錯(cuò)。
* 自動(dòng)配置之后要自定義配置需編碼javaConfig,需要了解這些配置類api。
* 版本迭代太快,新版本對(duì)老版本改動(dòng)太多導(dǎo)致不兼容,比如1.3.5之前的springBootTest和1.4.0之后的springBootTest。
只有合適的架構(gòu)才是最好的架構(gòu)如果能接受spring boot這些缺點(diǎn),spring boot確實(shí)是一個(gè)可以提高開發(fā)效率的不錯(cuò)的選擇。
啟動(dòng)流程
扯了這么多,該上正題了,讓我們來(lái)看看spring boot是怎樣啟動(dòng)和啟動(dòng)做了哪些事情。
以下代碼是spring boot項(xiàng)目標(biāo)準(zhǔn)的啟動(dòng)方式,使用注解@SpringBootApplication并且在main方法中調(diào)用SpringApplication的run方法,就可以完成。我們就從這個(gè)run方法開始看看spring boot的啟動(dòng)過(guò)程。
@SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
我們進(jìn)入run方法,可以看到最終是調(diào)用了 new SpringApplication(sources).run(args);new SpringApplication(sources).run(args); 這個(gè)方法,可以看到,springBoot的啟動(dòng)可以分為兩個(gè)部分,第一部分:SpringApplication的實(shí)例化;第二部分:調(diào)用該實(shí)例運(yùn)行run方法。我們先來(lái)看看這個(gè)SpringApplication的實(shí)例化過(guò)程。
private void initialize(Object[] sources) { if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources)); } //判定是否為webEnvironment this.webEnvironment = deduceWebEnvironment(); //實(shí)例化并加載所有可以加載的ApplicationContextInitializer setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); //實(shí)例化并加載所有可以加載的ApplicationListener setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); }
關(guān)鍵點(diǎn)在兩個(gè)set方法上**
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class))** 和 **setListeners((Collection)
getSpringFactoriesInstances(ApplicationListener.class))**
這兩個(gè)方法一毛一樣,挑實(shí)例化ApplicationContextInitializer講一講。
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { //拿到類加載器 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Use names and ensure unique to protect against duplicates //使用loadFactoryNames方法載入所有的ApplicationContextInitializer的類全限定名 Set<String> names = new LinkedHashSet<String>( SpringFactoriesLoader.loadFactoryNames(type, classLoader)); //使用反射將所有的ApplicationContextInitializer實(shí)例化 List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names); //排序 AnnotationAwareOrderComparator.sort(instances); return instances; }
自動(dòng)配置的關(guān)鍵就是這個(gè) getSpringFactoriesInstances方法,確切的說(shuō)是這個(gè)方法里的loadFactoryNames方法,浪我們看看這個(gè)loadFactoryNames方法干了啥,咋就能實(shí)現(xiàn)自動(dòng)配置。
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { Enumeration<URL> urls = classLoader != null?classLoader.getResources("META-INF/spring.factories"):ClassLoader.getSystemResources("META-INF/spring.factories"); ArrayList result = new ArrayList(); while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException var8) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + "META-INF/spring.factories" + "]", var8); } }
可以看到這個(gè)方法就做了一件事,就是從META-INF/spring.factories這個(gè)路徑取出所有”url”來(lái),我們可以去到這個(gè)路徑下看看到底是些啥?
# Initializers org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer # Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.boot.autoconfigure.BackgroundPreinitializer
這下大家都應(yīng)該明白了,spring是通過(guò)將所有你加載的jar包中找到它需要的ApplicationContextInitializer來(lái)進(jìn)行動(dòng)態(tài)的配置的,只要你有用到特定的maven包,初始化的時(shí)候會(huì)找這個(gè)包下的META-INF/spring.factories的需要的類比如ApplicationContextInitializer進(jìn)行實(shí)例化bean,你就可以用了,不需要任何配置。
說(shuō)到這已經(jīng)將所有SpringApplication實(shí)例化說(shuō)完了,只是在加載完ApplicationContextInitializer和ApplicationListener這之后還有一步,就是找到啟動(dòng)類所在的位置并且設(shè)入屬性mainApplicationClass中。
接下來(lái)讓我們回到new SpringApplication(sources).run(args)
方法來(lái)看看run方法是怎么run的。
public ConfigurableApplicationContext run(String... args) { //開啟啟動(dòng)計(jì)時(shí)器,項(xiàng)目啟動(dòng)完會(huì)打印執(zhí)行時(shí)間出來(lái) StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; FailureAnalyzers analyzers = null; configureHeadlessProperty(); //獲取SpringApplicationRunListener并啟動(dòng)監(jiān)聽器 SpringApplicationRunListeners listeners = getRunListeners(args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //環(huán)境變量的加載 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); //啟動(dòng)后console的打印出來(lái)的一堆配置信息 Banner printedBanner = printBanner(environment); //終極大boss->ApplicationContext實(shí)例化 context = createApplicationContext(); analyzers = new FailureAnalyzers(context); prepareContext(context, environment, listeners, applicationArguments, printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } return context; } catch (Throwable ex) { handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); } }
從這個(gè)方法里面做的最關(guān)鍵的三件事情就是:
獲取監(jiān)聽器并啟動(dòng)
加載環(huán)境變量,該環(huán)境變量包括system environment、classpath environment和用戶自己加的application.properties
創(chuàng)建ApplicationContext
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() == null); logStartupProfileInfo(context); } // Add boot specific singleton beans context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner != null) { context.getBeanFactory().registerSingleton("springBootBanner", printedBanner); } // Load the sources Set<Object> sources = getSources(); Assert.notEmpty(sources, "Sources must not be empty"); load(context, sources.toArray(new Object[sources.size()])); listeners.contextLoaded(context); }
前兩點(diǎn)沒什么好說(shuō)的,重點(diǎn)說(shuō)說(shuō)第三個(gè),創(chuàng)建ApplicationContext。創(chuàng)建applicationContext又分為幾部:實(shí)例化applicationContext、prepareContext、refreshContext。實(shí)例化applicationContext會(huì)根據(jù)在之前我們說(shuō)的webEnvironment這個(gè)屬性判斷是使用webContext類AnnotationConfigEmbeddedWebApplicationContext還是普通context類AnnotationConfigApplicationContext(在這里我們使用的是webContext為例)然后通過(guò)反射進(jìn)行實(shí)例化。applicationContext實(shí)例化完了會(huì)進(jìn)入prepareContext流程,這個(gè)prepareContext方法會(huì)加載之前準(zhǔn)備好的environment進(jìn)入context中,然后如果有beanNameGenerator和resourceLoader那么提前創(chuàng)建bean加載進(jìn)applicationContext,但是一般這兩個(gè)都是空的,所以直接進(jìn)入applyInitializers方法,將之前實(shí)例化的所有initializers進(jìn)行初始化,所有的bean就是在這里進(jìn)行bean的掃描和加載的因這次講的是啟動(dòng)過(guò)程,所以不再細(xì)講。最后把創(chuàng)建好的applicationContext設(shè)置進(jìn)入listener,prepareContext過(guò)程就結(jié)束了。最后是refreshContext,這個(gè)就和spring的bean加載過(guò)程一致了,bean的注入、beanFactory、postProcessBeanFactory等等,詳情可以去看看spring bean的生命周期。
總結(jié)
spring boot 初始化內(nèi)容還是很多的,但是總結(jié)起來(lái)就四點(diǎn):
* 創(chuàng)建SpringApplication實(shí)例,判定環(huán)境,是web環(huán)境還是普通環(huán)境。加載所有需要用到的Initializers和Listeners,這里使用約定大于配置的理念揭開了自動(dòng)配置的面紗。
* 加載環(huán)境變量,環(huán)境變量包括system environment、classpath environment、application environment(也就是我們自定義的application.properties配置文件)
* 創(chuàng)建SpringApplicationRunListeners
* 創(chuàng)建ApplicationContext,設(shè)置裝配context,在這里將所有的bean進(jìn)行掃描最后在refreshContext的時(shí)候進(jìn)行加載、注入。最終將裝配好的context作為屬性設(shè)置進(jìn)SpringApplicationRunListeners,這就完成了一個(gè)spring boot項(xiàng)目的啟動(dòng)。
以上所述是小編給大家介紹的Spring Boot啟動(dòng)流程,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java如何使用Jetty實(shí)現(xiàn)嵌入式的Servlet容器
這篇文章主要介紹了Java使用Jetty實(shí)現(xiàn)嵌入式的Servlet容器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們來(lái)一起了解一下吧2019-06-06SpringBoot項(xiàng)目運(yùn)行一段時(shí)間后自動(dòng)關(guān)閉的坑及解決
這篇文章主要介紹了SpringBoot項(xiàng)目運(yùn)行一段時(shí)間后自動(dòng)關(guān)閉的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09mybatis注解動(dòng)態(tài)sql注入map和list方式(防sql注入攻擊)
這篇文章主要介紹了mybatis注解動(dòng)態(tài)sql注入map和list方式(防sql注入攻擊),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-11-11Springboot如何使用OSHI獲取和操作系統(tǒng)和硬件信息
這篇文章主要介紹了Springboot如何使用OSHI獲取和操作系統(tǒng)和硬件信息問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10nacos在liunx系統(tǒng)中啟動(dòng)成功瀏覽器卻訪問不了的解決方法
在linux下搭建nacos,現(xiàn)在想要啟動(dòng),訪問nacos頁(yè)面,訪問不了,所以本文小編將給大家介紹nacos在liunx系統(tǒng)中啟動(dòng)成功,瀏覽器卻訪問不了?全面的解決辦法,需要的朋友可以參考下2023-09-09Java 實(shí)戰(zhàn)項(xiàng)目之疫情防控管理系統(tǒng)詳解
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)疫情防控管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11SpringBoot項(xiàng)目Jar包如何瘦身部署的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot項(xiàng)目Jar包如何瘦身部署的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09linux用java -jar啟動(dòng)jar包緩慢的問題
這篇文章主要介紹了linux用java -jar啟動(dòng)jar包緩慢的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-09-09