SpringBoot源碼學習系列之啟動原理簡介

本部落格通過debug方式簡單跟一下Springboot application啟動的源碼,Springboot的啟動源碼是比較複雜的,本部落格只是簡單梳理一下源碼,淺析其原理

為了方便跟源碼,先找個Application類,打個斷點,進行調試,如圖所示:

step into,run方法調用了SpringApplication的run方法

通過debug,Springboot啟動過程,會先執行如下關鍵的構造函數

分析構造函數源碼:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {  		this.resourceLoader = resourceLoader;  		Assert.notNull(primarySources, "PrimarySources must not be null");  		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));  		// 判斷當前的web類型  		this.webApplicationType = WebApplicationType.deduceFromClasspath();  		//設置初始化的ApplicationInitializer類,從類路徑下面的META-INF/spring.factories配置文件獲取所有的ApplicationInitializer保存起來  		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));  		//同理,從類路徑下面的META-INF/spring.factories配置文件獲取所有的ApplicationListener保存起來  		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));  		//從多個配置類中找到有main方法的主配置類  		this.mainApplicationClass = deduceMainApplicationClass();  	}

注意:上面過程其實就是創建Springboot的Application啟動類的過程

deduceFromClasspath方法是判斷web類型的

繼續debug ApplicationContextInitializer這些Initializer類,可以說是初始化類的設置過程

SpringFactoriesLoader.loadFactoryNames(type, classLoader)獲取所有的Initializer類的類名

Evaluate可以看出掃描到如下的類

繼續debug,這個是Spring框架的底層類

找到主要的源碼,loadSpringFactories方法也是從如下的位置獲取配置資訊的

從META-INF/spring.factories獲取對應的配置資訊

框架的文件位置在autoconfiguration工程里,顯然如果要自定義Initializer類的話,自己新建一些Initializer類,然後自己寫個META-INF/spring.factories類,也是可以被掃描到的

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {  		//用一個ConcurrentReferenceHashMap來快取資訊  		MultiValueMap<String, String> result = cache.get(classLoader);  		if (result != null) { //快取讀取到配置資訊,返回快取數據  			return result;  		}  		// 快取讀取不到的情況,重新從META-INF/spring.factories配置文件讀取  		try {  			Enumeration<URL> urls = (classLoader != null ?  					classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :  					ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));  			result = new LinkedMultiValueMap<>();  			// 遍歷循環讀取配置資訊  			while (urls.hasMoreElements()) {  				URL url = urls.nextElement();  				UrlResource resource = new UrlResource(url);  				// 用PropertiesLoaderUtils工具類讀取資源文件  				Properties properties = PropertiesLoaderUtils.loadProperties(resource);  				for (Map.Entry<?, ?> entry : properties.entrySet()) {  				//獲取到Initializer對應的全類名  					String factoryTypeName = ((String) entry.getKey()).trim();  					for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {  						result.add(factoryTypeName, factoryImplementationName.trim());  					}  				}  			}  			// 重新放在快取里  			cache.put(classLoader, result);  			return result;  		}  		catch (IOException ex) {  			throw new IllegalArgumentException("Unable to load factories from location [" +  					FACTORIES_RESOURCE_LOCATION + "]", ex);  		}  	}

ApplicationInitializer類的全類名都被掃描到之後,返回剛才的源碼,繼續看看,如圖,從命名看應該是進行類的實例化過程

step into,果然是的,還是調用了Spring框架的底層工具類,BeanUtils進行類的實例化過程

setListeners方法的過程同理,本文就不詳細分析:

繼續往下debug,deduceMainApplicationClass方法

private Class<?> deduceMainApplicationClass() {  		try {  			//獲取運行時的堆棧屬性數組  			StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();  			for (StackTraceElement stackTraceElement : stackTrace) {  				//有main方法的Application類返回  				if ("main".equals(stackTraceElement.getMethodName())) {  					return Class.forName(stackTraceElement.getClassName());  				}  			}  		}  		catch (ClassNotFoundException ex) {  			// Swallow and continue  		}  		return null;  	}

獲取到的就是創建Springboot工程時的Application類

Springboot的Application類創建成功之後,才真正開始執行run方法

public ConfigurableApplicationContext run(String... args) {  		StopWatch stopWatch = new StopWatch();  		stopWatch.start();  		ConfigurableApplicationContext context = null;  		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();  		//校驗java.awt.headless的  		configureHeadlessProperty();  		//從META-INF/spring.factories獲取SpringApplicationRunListeners,和前面的分析同理,本文就不詳細介紹  		SpringApplicationRunListeners listeners = getRunListeners(args);  		//回調SpringApplicationRunListeners 的starting方法  		listeners.starting();  		try {  			//封裝命令行參數  			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);  			//準備環境,環境創建完成之後,再回調SpringApplicationRunListeners 的environmentPrepared方法,表示環境準備好  			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);  			configureIgnoreBeanInfo(environment);  			//控制台列印Banner資訊的,後面再簡單分析  			Banner printedBanner = printBanner(environment);  			// 創建Spring的IOC容器,創建過程比較複雜,會分析是web類型的ioc容器,還是普通的ioc容器等等  			context = createApplicationContext();  			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,  					new Class[] { ConfigurableApplicationContext.class }, context);  			//將environment保存到ioc,執行applyInitializers方法,applyInitializers方法執行完成之後,再回調SpringApplicationRunListeners的contextPrepared方法  			//applyInitializers方法作用:回調之前保存的所有的ApplicationContextInitializer的initialize方法  			prepareContext(context, environment, listeners, applicationArguments, printedBanner);  			//刷新ioc容器,其實就是ioc容器的初始化過程,還沒進行屬性設置,後置處理器,僅僅是掃描、創建、載入所有組件等等過程  			refreshContext(context);  			afterRefresh(context, applicationArguments);  			stopWatch.stop();  			if (this.logStartupInfo) {  				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);  			}  			//回調所有SpringApplicationRunListener的started方法  			listeners.started(context);  			//從ioc容器中獲取所有的ApplicationRunner和CommandLineRunner進行回調,ApplicationRunner先回調,CommandLineRunner再回調  			callRunners(context, applicationArguments);  		}  		catch (Throwable ex) {  			handleRunFailure(context, ex, exceptionReporters, listeners);  			throw new IllegalStateException(ex);  		}    		try {  			//回調所有SpringApplicationRunListener的running方法  			listeners.running(context);  		}  		catch (Throwable ex) {  			handleRunFailure(context, ex, exceptionReporters, null);  			throw new IllegalStateException(ex);  		}  		//Springboot應用啟動成功後,才返回啟動的ioc容器  		return context;  	}

回顧一下前面源碼的環境準備方法,找重點程式碼,如圖,可以看出環境準備完成後會回調SpringApplicationRunListener的environmentPrepared方法,表示環境準備完成

banner列印的方法,如圖,執行完成,控制台的banner資訊就列印出來了:

ioc初始化之前,會執行applyInitializers方法,執行完成後,再回調SpringApplicationRunListener的contextPrepared方法

applyInitializers():回調之前保存的所有的ApplicationContextInitializer的initialize方法

從ioc容器中獲取所有的ApplicationRunner和CommandLineRunner進行回調

ok,從源碼的簡單分析,可以看出有幾個重要的事件監聽機制,下面引用尚矽谷影片的例子:

只需要放在ioc容器中的有:

  • ApplicationRunner
@Component  public class HelloApplicationRunner implements ApplicationRunner {      @Override      public void run(ApplicationArguments args) throws Exception {          System.out.println("ApplicationRunner...run....");      }  }
  • CommandLineRunner
@Component  public class HelloCommandLineRunner implements CommandLineRunner {      @Override      public void run(String... args) throws Exception {          System.out.println("CommandLineRunner...run..."+ Arrays.asList(args));      }  }

配置在META-INF/spring.factories的有:

  • ApplicationContextInitializer
public class HelloApplicationContextInitializer implements  ApplicationContextInitializer<ConfigurableApplicationContext> {      @Override      public void initialize(ConfigurableApplicationContext applicationContext) {     System.out.println("ApplicationContextInitializer...initialize..."+applicationContext);      }  }
  • SpringApplicationRunListener
package com.example.springboot.web.listener;    import org.springframework.boot.SpringApplication;  import org.springframework.boot.SpringApplicationRunListener;  import org.springframework.context.ConfigurableApplicationContext;  import org.springframework.core.env.ConfigurableEnvironment;    public class HelloSpringApplicationRunListener implements SpringApplicationRunListener {        //必須有的構造器      public HelloSpringApplicationRunListener(SpringApplication application, String[] args){        }        @Override      public void starting() {          System.out.println("SpringApplicationRunListener...starting...");      }        @Override      public void environmentPrepared(ConfigurableEnvironment environment) {          Object o = environment.getSystemProperties().get("os.name");          System.out.println("SpringApplicationRunListener...environmentPrepared.."+o);      }        @Override      public void contextPrepared(ConfigurableApplicationContext context) {          System.out.println("SpringApplicationRunListener...contextPrepared...");      }        @Override      public void contextLoaded(ConfigurableApplicationContext context) {          System.out.println("SpringApplicationRunListener...contextLoaded...");      }      }

配置(META-INF/spring.factories)

org.springframework.context.ApplicationContextInitializer=  com.example.springboot.web.listener.HelloApplicationContextInitializer    org.springframework.boot.SpringApplicationRunListener=  com.example.springboot.web.listener.HelloSpringApplicationRunListener

例子下載:github下載鏈接