SpringBoot的入口类
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
从入口类可以看出,在Main方法中,执行了run;那下来进入run方法看一下
run方法代码
很显然,这是一个重载方法,作用就是像红框标注的一样,静态帮助方法;再往下走
重点来了:这里分两步分析,先说大概说下这两步都做了什么。
第一步:
从代码中可以看到,有效代码就7行,那么在第一步中做了7件事情:
1、资源加载器初始化为null
this.resourceLoader = resourceLoader;
2、 断言主资源加载器不能为null
Assert.notNull(primarySources, "PrimarySources must not be null");
3、 将主资源加载器放到资源加载器中并去重(Set自带去重功能)
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
4、 推断web应用类型
this.webApplicationType = deduceWebApplicationType();
5、 应用上下文工厂实例初始化
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
6、 应用监听器工厂实例初始化
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
7、 推断应用入口main方法的主类
this.mainApplicationClass = deduceMainApplicationClass();
分析完第一步的大框后,先不着急去看每一步的具体逻辑,下面来看第二步的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;
}
一点一点分析:
1、创建并启动监听类;
2、 初始化应用上下文和异常报告结合
3、 设置系统属性“java.awt.headless”的值,默认为true
4、 获取应用运行监听器,并启动监听器
5、初始化默认应用参数类
6、 根据运行监听器和应用参数类准备环境
7、 创建Banner打印类
8、 创建应用上下文
9、 根据上下文获取异常报告器
10、根据环境、监听器、应用参数、banner准备应用上下文
11、刷新应用上下文
12、刷新完应用上下文后续操作
13、停止计时监听类
14、输出日志记录执行主类名、时间信息
15、发布应用上下文启动完成事件
16、执行所有runner运行器
17、发布应用上下文就绪事件
18、返回上下文
至此,这两步大框分析完毕!具体内部实现,有空再继续写吧!
参考: