Spring Boot啟動原理

2021-09-13 21:03:42 字數 2422 閱讀 5822

如何啟動乙個springboot應用?

public class test

}

原始碼1

initialize(sources); }

原始碼2(initialize(…))

private void initialize(object sources) 

//判斷當前應用是否為web應用

this.webenvironment = deducewebenvironment();

setinitializers((collection) getspringfactoriesinstances(

//從多個配置類中找到有main方法的主配置類

}

原始碼3 getspringfactoriesinstances(…)

獲取通過meta-inf/spring.factories檔案獲取到的對應的類路徑下的例項

private collection<? extends t> getspringfactoriesinstances(classtype) );

} private collection<? extends t> getspringfactoriesinstances(classtype,

class<?> parametertypes, object... args)

從執行類的堆疊中獲取含有main方法的類

//從執行類的堆疊中獲取含有main方法的類

try

}} catch (classnotfoundexception ex)

return null;

}

原始碼1(run(…))

run方法

//跑馬燈物件--用於記錄執行時間

stopwatch stopwatch = new stopwatch();

stopwatch.start();

failureanalyzers analyzers = null;

configureheadlessproperty();

listeners.starting();

try

return context;

} catch (throwable ex) }

原始碼2(getrunlisteners(…)) }

原始碼3(prepareenvironment(…))

準備環境

//準備環境

private configurableenvironment prepareenvironment(

// create and configure the environment

//建立環境

configurableenvironment environment = getorcreateenvironment();

//配置環境

listeners.environmentprepared(environment);

if (!this.webenvironment)

return environment;

}

根據建立的環境判斷,是建立web容器,還是普通的ioc容器

//根據建立的環境判斷,是建立web容器,還是普通的ioc容器

if (contextclass == null)

catch (classnotfoundexception ex)

} }

原始碼5(preparecontext(…))

準備上下文

//準備上下文

//設定環境

context.setenvironment(environment);

listeners.contextprepared(context);

if (this.logstartupinfo)

// add boot specific singleton beans

if (printedbanner != null)

// load the sources

setsources = getsources();

assert.notempty(sources, "sources must not be empty");

load(context, sources.toarray(new object[sources.size()]));

listeners.contextloaded(context);

}

執行run方法,通過配置檔案meta-inf/spring.factories找到對應的***路徑,將其實例化並啟動。建立環境和建立上下文,並為***配置。重新整理上下文,然後呼叫所有的***的finished()方法,廣播springboot已經完成了。

springboot 啟動原理

public static void main string args throws exception 作為深入原理的第一篇,我們先來看下spring boot應用是怎麼啟動的。public run string args return context catch throwableex 初始化 ...

SpringBoot 啟動原理

public static void main string args springboot的啟動相比於傳統的spring過程來說是超超級的方便的說,如上 private void initialize object sources setinitializers collection getspr...

springboot啟動原理

springboot專案一般都是打包成jar包直接執行main方法啟動,當然也可以跟傳統的專案一樣打包war包放在tomcat裡面啟動.那麼springboot怎麼直接通過main方法啟動呢?舉個栗子,這是乙個簡單的main方法啟動類 enableasync enablescheduling ena...