springboot 啟動流程

2021-10-19 06:29:28 字數 3245 閱讀 1133

public

(class<

?>

primarysources, string[

] args)

public

(resourceloader resourceloader, class<

?>..

. primarysources)

(string.

.. args)

, context)

;//方法,主要是呼叫所有初始化類的 #initialize(...) 方法//主要是給context屬性賦值

preparecontext

;refreshcontext

(context)

;//呼叫spring的refresh,並且會啟動tomcat(onrefresh)

afterrefresh

;//執行 spring 容器的初始化的後置邏輯。預設實現為空

stopwatch.

stop()

;if(this

.logstartupinfo)

listeners.

started

(context)

;callrunners;}

catch

(throwable ex)

trycatch

(throwable ex)

return context;

}

private configurableenvironment prepareenvironment

configurationpropertysources.

attach

(environment)

;return environment;

}

這裡主要是對environment(環境)進行一些初始化,並且會在configureenvironment這個方法裡面獲取啟動引數,加入環境

protected

void

}

private

void

refreshcontext

catch

(accesscontrolexception ex)

}//spring

refresh

(context)

;}

在spring 的onrefresh()

protected

void

onrefresh()

catch

(throwable ex)

}

private

void

createwebserver()

else

if(servletcontext != null)

initpropertysources()

;}

這裡主要是走if的邏輯,從servletwebserve***ctory 中獲取webserver的例項

那麼servletwebserve***ctory 是什麼?

在這個介面下有一些例項,例如tomcat對應的例項tomcatservletwebserve***ctory,就是用來生產tomcat的物件

@override

public webserver getwebserver

(servletcontextinitializer.

.. initializers)

tomcat tomcat =

newtomcat()

;//建立tomcat

file basedir =

(this

.basedirectory != null)

?this

.basedirectory :

createtempdir

("tomcat");

tomcat.

setbasedir

(basedir.

getabsolutepath()

);connector connector =

newconnector

(this

.protocol)

; connector.

setthrowonfailure

(true);

tomcat.

getservice()

.addconnector

(connector)

;customizeconnector

(connector)

; tomcat.

setconnector

(connector)

; tomcat.

gethost()

.setautodeploy

(false);

configureengine

(tomcat.

getengine()

);//!!

for(connector additionalconnector :

this

.additionaltomcatconnectors)

preparecontext

(tomcat.

gethost()

, initializers)

;return

gettomcatwebserver

(tomcat)

;//開啟tomcat

}

這裡非常直觀的可以看到直接new 了乙個tomcat物件

public

tomcatwebserver

(tomcat tomcat,

boolean autostart)

private

void

initialize()

throws webserverexception })

;// start the server to trigger initialization listeners

this

.tomcat.

start()

; 。。。。

}

然後再這個initialize()裡面啟動tomcat

springboot 啟動流程

this.resourceloader resourceloader assert.notnull primarysources,primarysources must not be null this.primarysources new linkedhashset arrays.aslist p...

springboot啟動流程

本文以除錯乙個實際的springboot啟動程式為例,參考流程中主要類類圖,來分析其啟動邏輯和自動化配置原理。2.載入springboot配置環境 configurableenvironment 如果是通過web容器發布,會載入standardenvironment,其最終也是繼承了configur...

Spring Boot啟動流程

隨著我們ctrl加滑鼠左鍵的使用,最終我們會在乙個springboot專案中發現啟動過程由以下兩個過程組成 構造過程 public resourceloader resourceloader,class primarysources run方法 string.args context 這個也很重要,...