通過上一章的源碼分析,我們知道了spring boot裡面的listeners到底是什麼(META INF/spring.factories定義的資源的實例),以及它是創建和啟動的,今天我們繼續深入分析一下SpringApplication實例變數中的run函數中的其他內容。還是先把run函數的代碼 ...
通過上一章的源碼分析,我們知道了spring boot裡面的listeners到底是什麼(META-INF/spring.factories定義的資源的實例),以及它是創建和啟動的,今天我們繼續深入分析一下SpringApplication實例變數中的run函數中的其他內容。還是先把run函數的代碼貼出來:
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
在listeners啟動了以後,我們來看一下ApplicationArguments applicationArguments
= new DefaultApplicationArguments(args); 在DefaultApplicationArguments的構造函數里,我們跟蹤過去發現其最終調用的SimpleCommandLineArgsParser.parse函數:
public CommandLineArgs parse(String... args) {
CommandLineArgs commandLineArgs = new CommandLineArgs();
String[] var3 = args;
int var4 = args.length;
for(int var5 = 0; var5 < var4; ++var5) {
String arg = var3[var5];
if(arg.startsWith("--")) {
String optionText = arg.substring(2, arg.length());
String optionValue = null;
String optionName;
if(optionText.contains("=")) {
optionName = optionText.substring(0, optionText.indexOf(61));
optionValue = optionText.substring(optionText.indexOf(61) + 1,
optionText.length());
} else {
optionName = optionText;
}
if(optionName.isEmpty() || optionValue != null && optionValue.isEmpty()) {
throw new IllegalArgumentException("Invalid argument syntax: " + arg);
}
commandLineArgs.addOptionArg(optionName, optionValue);
} else {
commandLineArgs.addNonOptionArg(arg);
}
}
return commandLineArgs;
}
從這段代碼中我們看到DefaultApplicationArguments其實是讀取了命令行的參數。
小發現:通過分析這個函數的定義,你是不是想起了spring boot啟動的時候,用命令行參數自定義埠號的情景?
java -jar MySpringBoot.jar --server.port=8000
接著往下看:ConfigurableEnvironment environment = this.prepareEnvironment(listeners, ex);
通過這行代碼我們可以看到spring boot把前面創建出來的listeners和命令行參數,傳遞到prepareEnvironment函數中來準備運行環境。來看一下prepareEnvironment函數的真面目:
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (this.webApplicationType == WebApplicationType.NONE) {
environment = new EnvironmentConverter(getClassLoader())
.convertToStandardEnvironmentIfNecessary(environment);
}
ConfigurationPropertySources.attach(environment);
return environment;
}
在這裡我們看到了環境是通過getOrCreateEnvironment創建出來的,再深挖一下getOrCreateEnvironment的源碼:
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
}
if (this.webApplicationType == WebApplicationType.SERVLET) {
return new StandardServletEnvironment();
}
return new StandardEnvironment();
}
通過這段代碼我們看到瞭如果environment 已經存在,則直接返回當前的環境。
小思考:在什麼情況下會出現environment 已經存在的情況?提示:我們前面講過,可以自己初始化SpringApplication,然後調用run函數,在初始化SpringApplication和調用run函數之間,是不是可以發生點什麼?
下麵的代碼判斷了webApplicationType是不是SERVLET,如果是,則創建Servlet的環境,否則創建基本環境。我們來挖一挖webApplicationType是在哪裡初始化的:
private static final String REACTIVE_WEB_ENVIRONMENT_CLASS = "org.springframework."
+ "web.reactive.DispatcherHandler";
private static final String MVC_WEB_ENVIRONMENT_CLASS = "org.springframework."
+ "web.servlet.DispatcherServlet";
/**
* Create a new {@link SpringApplication} instance. The application context will load
* beans from the specified primary sources (see {@link SpringApplication class-level}
* documentation for details. The instance can be customized before calling
* {@link #run(String...)}.
* @param resourceLoader the resource loader to use
* @param primarySources the primary bean sources
* @see #run(Class, String[])
* @see #setSources(Set)
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = deduceWebApplicationType();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
private WebApplicationType deduceWebApplicationType() {
if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
return WebApplicationType.REACTIVE;
}
for (String className : WEB_ENVIRONMENT_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
return WebApplicationType.NONE;
}
}
return WebApplicationType.SERVLET;
}
通過這段代碼,我們發現了原來spring boot是通過檢查當前環境中是否存在
org.springframework.web.servlet.DispatcherServlet類來判斷當前是否是web環境的。
接著往下看,獲得了ConfigurableEnvironment環境以後,通過後面的代碼對環境進行“微調”。
通過this.configureIgnoreBeanInfo(environment);如果System中的spring.beaninfo.ignore屬性為空,就把當前環境中的屬性覆蓋上去:
private void configureIgnoreBeanInfo(ConfigurableEnvironment environment) {
if(System.getProperty("spring.beaninfo.ignore") == null) {
Boolean ignore = (Boolean)environment.getProperty("spring.beaninfo.ignore",
Boolean.class, Boolean.TRUE);
System.setProperty("spring.beaninfo.ignore", ignore.toString());
}
}
通過Banner printedBanner = this.printBanner(environment);這行代碼列印出spring boot的Banner。還記得spring boot啟動的時候,在控制台顯示的那個圖片嗎?這裡不作深究,繼續往下看:
context = this.createApplicationContext();創建了應用上下文:
public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context."
+ "annotation.AnnotationConfigApplicationContext";
public static final String DEFAULT_WEB_CONTEXT_CLASS = "org.springframework.boot."
+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass",
ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
通過這裡我們看到,spring boot是根據不同的webApplicationType的類型,來創建不同的ApplicationContext的。
總結:通過上面的各種深挖,我們知道了spring boot 2.0中的環境是如何區分普通環境和web環境的,以及如何準備運行時環境和應用上下文。時間不早了,今天就跟大家分享到這裡,下一篇文章會繼續跟大家分享spring boot 2.0源碼的實現。