首先我們要先帶著我們的疑問,spring boot是如何啟動應(yīng)用程序?去分析SpringBoot的啟動源碼。
我們在新建SpringBoot項目時,核心方法就是主類的run方法。
SpringApplication.run(ArchWebApplication.class, args)
我們點擊run方法進入到源碼中,這塊傳入的了一個我們當(dāng)前程序主類的類對象以及主程序參數(shù)。
public static ConfigurableApplicationContext run(Class<?> primarySource,
String... args) {
return run(new Class<?>[] { primarySource }, args);
}
再往下走,調(diào)用本身的run方法,這里就開始初始化SpringApplication對象啦,然后在調(diào)用run方法。初始化SpringApplication對象時也是將主類的類對象傳入進去,然后調(diào)用run方法,將主程序傳進來的參數(shù)傳進去。
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
我們先來看SpringApplication對象初始化時都做了哪些操作。
同樣調(diào)用自身的雙參構(gòu)造方法,null為傳入的資源加載器。
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
再往下走就到了初始化SpringApplication的核心邏輯啦。
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
//首先是將傳來的資源加載器進行賦值,當(dāng)然我們知道這個資源加載器是null
this.resourceLoader = resourceLoader;
//然后在進行類對象的判空
Assert.notNull(primarySources, "PrimarySources must not be null");
//然后將傳進來的類對像的數(shù)組轉(zhuǎn)成list在轉(zhuǎn)成set。
//(我估計這里是為了去重類對象,因為可以穿進來的可變參數(shù)有重復(fù)的,可變參數(shù)實質(zhì)就是一個數(shù)組)。
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//deduceFromClasspath 方法的目的是用來判斷應(yīng)用是servlet還是reactive應(yīng)用
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//這一步的邏輯是從spring.factories文件中讀取 key為ApplicationContextInitializer的類信息,采用反射實例化對象
//設(shè)置上下文信息
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
//這一步的邏輯同上,也是從spring.factories文件中讀取 key為ApplicationListener的類信息,采用反射實例化對象
//設(shè)置監(jiān)聽器
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//判斷是否為main函數(shù),配置主函數(shù)啟動類 class信息
this.mainApplicationClass = deduceMainApplicationClass();
}
上面是一個創(chuàng)建SpringApplication的整體邏輯,那么我們在具體看一下 WebApplicationType.deduceFromClasspath()
里面的邏輯是怎么樣的。WebApplicationType本身是一個枚舉類。
//一共三種方式返回服務(wù)條件的一種
static WebApplicationType deduceFromClasspath() {
//判斷當(dāng)前應(yīng)用是不是 REACTIVE應(yīng)用
if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
&& !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
//判斷是不是 非web應(yīng)用
for (String className : SERVLET_INDICATOR_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}
//都不是的話返回 web應(yīng)用方式
return WebApplicationType.SERVLET;
}
ClassUtils.isPresent()
這個方法的主要作用是通過反射判斷相應(yīng)的類存不存在。
public static boolean isPresent(String className, @Nullable ClassLoader classLoader) {
try {
forName(className, classLoader);
return true;
}
catch (IllegalAccessError err) {
throw new IllegalStateException("Readability mismatch in inheritance hierarchy of class [" +
className + "]: " + err.getMessage(), err);
}
catch (Throwable ex) {
// Typically ClassNotFoundException or NoClassDefFoundError...
return false;
}
}
ok,分析完WebApplicationType.deduceFromClasspath()
,我們在來看一下getSpringFactoriesInstances()
這個方法的核心邏輯。
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
Class<?>[] parameterTypes, Object... args) {
//獲取ClassLoader
ClassLoader classLoader = getClassLoader();
//這一步很重要,重點在于內(nèi)部是從spring.factories中獲取對應(yīng)的類信息
Set<String> names = new LinkedHashSet<>(
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
//等到需要加載的類信息之后,通過反射創(chuàng)建對象。
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
classLoader, args, names);
//排序
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
我們來看一下,loadFactoryNames
中都干了什么,它這里面的核心就在于加載配置文件,反射實例化對象
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}
//核心邏輯在這個方法
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
//首先先去判斷下Map中是否有值,有值的話,就直接返回,相當(dāng)于一個本地緩存。
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
//如果Map沒有值的話,獲取資源目錄下的spring.factories文件。加載配置
//源碼中 FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
//下面就是遍歷放進map中
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryClassName = ((String) entry.getKey()).trim();
for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
result.add(factoryClassName, factoryName.trim());
}
}
}
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}
OK,這里關(guān)于SpringApplication初始化的操作就已經(jīng)完成啦,那么下面我們在一下run()
方法里都做些什么操作。傳進去的是主程序的參數(shù)。
public ConfigurableApplicationContext run(String... args) {
//創(chuàng)建StopWatch對象,用于記錄服務(wù)啟動的時間
StopWatch stopWatch = new StopWatch();
//記錄服務(wù)啟動開始時間
stopWatch.start();
//定義應(yīng)用程序上下文
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//配置運行程序的系統(tǒng)環(huán)境,以確保可正確的運行。
configureHeadlessProperty();
//獲取在SpringApplication上的所有監(jiān)聽器。
SpringApplicationRunListeners listeners = getRunListeners(args);
//通知所有監(jiān)聽器,啟動應(yīng)用程序
listeners.starting();
try {
//封裝應(yīng)用程序的主程序參數(shù)
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
//準(zhǔn)備應(yīng)用環(huán)境,生成環(huán)境變量
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
//打印應(yīng)用程序的banner
Banner printedBanner = printBanner(environment);
//創(chuàng)建應(yīng)用上下文對象
context = createApplicationContext();
//從spring.factories中獲取SpringBootExceptionReporter類型的異常解析器。
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//用已有的數(shù)據(jù)準(zhǔn)備上下文,為刷新做準(zhǔn)備
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//啟動應(yīng)用程序上下文,通過refresh實現(xiàn)
refreshContext(context);
//上下文刷新后執(zhí)行一些后置處理
afterRefresh(context, applicationArguments);
//記錄結(jié)束時間
stopWatch.stop();
//判斷是否需要記錄應(yīng)用程序的啟動信息
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
//通知所有監(jiān)聽器,應(yīng)用程序已經(jīng)啟動,傳遞上下文對象和啟動時間
listeners.started(context);
//運行所有已經(jīng)注冊的runner
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//發(fā)布應(yīng)用上下文就緒事件
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
ok,下面我們來斷點調(diào)試下springboot啟動類的源碼執(zhí)行。
啟動SpringBoot應(yīng)用程序。
后面就是排序?qū)嵗?,返回實例。
剩下的就是我們上面畫的那些流程啦。
文章來源:http://www.zghlxwxcb.cn/news/detail-463866.html
ok,至此SpringBoot啟動的全流程就已經(jīng)完成啦,最后在總結(jié)一下大體的流程。文章來源地址http://www.zghlxwxcb.cn/news/detail-463866.html
- 初始化SpringApplication,運行SpringApplication的run方法
- 讀取 spring.factories 的多個初始化器和監(jiān)聽器
- 配置項目中環(huán)境變量、jvm配置信息、配置文件信息
- 預(yù)初始化環(huán)境,創(chuàng)建環(huán)境對象
- 創(chuàng)建Spring容器對象(ApplicationContext)
- 調(diào)用spring的refresh加載IOC容器、自動配置類,并創(chuàng)建bean等信息
- 調(diào)用很多監(jiān)聽器并傳遞上下文對象
- 運行相關(guān)runner
到了這里,關(guān)于【框架源碼】SpringBoot核心源碼解讀之啟動類源碼分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!