springboot專案一般都是打包成jar包直接執行main方法啟動,當然也可以跟傳統的專案一樣打包war包放在tomcat裡面啟動.那麼springboot怎麼直接通過main方法啟動呢?
舉個栗子,這是乙個簡單的main方法啟動類:
@enableasync
@enablescheduling
@enabletransactionmanagement
@enableconfigurationproperties
@enablecaching
(value =
)public
class
}
原始碼:public
(class<
?>
primarysources, string[
] args)
原始碼:public
(resourceloader resourceloader, class<
?>..
. primarysources)
private class<
?>()
}}catch
(classnotfoundexception ex)
return null;
}
(string.
.. args)
, context)
;//準備容器
preparecontext
;//重新整理容器
refreshcontext
(context)
;//spring容器後置處理
afterrefresh
;//計時終止
stopwatch.
stop()
;if(this
.logstartupinfo)
//結束通知
listeners.
started
(context)
;//執行runner
callrunners;}
catch
(throwable ex)
trycatch
(throwable ex)
//返回容器
return context;
}getrunlisteners方法,starting方法,獲取啟動監聽,和啟動
原始碼:(string[
] args)
;return
new(logger,
getspringfactoriesinstances
class
, types,
this
, args));
}private
collection
getspringfactoriesinstances
(class
type, class<
?>
parametertypes, object.
.. args)
prepareenvironment準備環境
列印banner (//準備啟動引數就不說了
new(args)
;private configurableenvironment prepareenvironment
//配置propertysources-如果有attach到environment
configurationpropertysources.
attach
(environment)
;return environment;
}
preparecontext,準備容器
refreshcontext,afterrefresh重新整理容器,重新整理容器之後執行方法private
void
preparecontext
// add boot specific singleton beans
//獲取beanfactory,註冊相關bean
configurablelistablebeanfactory beanfactory = context.
getbeanfactory()
; beanfactory.
registersingleton(;
if(printedbanner != null)
if(beanfactory instanceof
defaultlistablebeanfactory
)//延遲載入if(
this
.lazyinitialization)
// load the sources
setsources =
getallsources()
; assert.
notempty
(sources,
"sources must not be empty");
//載入啟動類
load
(context, sources.
toarray
(new
object[0
]));
listeners.
contextloaded
(context)
;}
就是spring容器的重新整理方法
springboot 啟動原理
public static void main string args throws exception 作為深入原理的第一篇,我們先來看下spring boot應用是怎麼啟動的。public run string args return context catch throwableex 初始化 ...
Spring Boot啟動原理
如何啟動乙個springboot應用?public class test 原始碼1 initialize sources 原始碼2 initialize private void initialize object sources 判斷當前應用是否為web應用 this.webenvironment...
SpringBoot 啟動原理
public static void main string args springboot的啟動相比於傳統的spring過程來說是超超級的方便的說,如上 private void initialize object sources setinitializers collection getspr...