nstances(ApplicationListener.class));
//找出main类,也就是SpringBoot项目的主类
this.mainApplicationClass = deduceMainApplicationClass();
}
2.1 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;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
//创建应用监听器
SpringApplicationRunListeners listeners = getRunListeners(args);
//开始监听
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//加载SpringBoot配置环境ConfigurableEnvironment,见2.2配置ConfigurableEnvironment
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
//打印banner
Banner printedBanner = printBanner(environment);
//创建应用程序上下文,见2.3 创建应用程序上下文
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}
2.2 配置ConfigurableEnvironment
加载SpringBoot配置环境ConfigurableEnvironment流程如下:
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
if (!this.webEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertToStandardEnvironmentIfNecessary(environment);
}
return environment;
}
在加载配置环境的过程中会判断是否是web容器启动,如果是容器启动会加载StandardServletEnvironment
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
}
if (this.webEnvironment) {
return new StandardServletEnvironment();
}
return new StandardEnvironment();
}
StandardServletEnvironment类的继承关系如下,StandardServletEnvironment
PropertyResolver接口是用于解析任何基础源的属性的接口,在加载完配置之后会将配置环境加入到监听器对象SpringApplicationRunListeners中。
2.3 创建应用程序上下文
然后会创建应用上下文对象,具体代码如下:
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
contextClass = Class.forName(this.webEnvironment
? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass",
ex);
}
}
retur