SpringBoot 源码解析
SpringApplication.java
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.webApplicationType = WebApplicationType.deduceFromClasspath();//判断当前类是WEB servlet
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));//加载所有的初始化器
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));//加载所有的监听器
this.mainApplicationClass = this.deduceMainApplicationClass();//设置程序运行的主类
}
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();//创建计时器,开始计时
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();//handless系统属性设置
SpringApplicationRunListeners listeners = this.getRunListeners(args);//初始化监听器
listeners.starting();//启动所有的监听器
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//设置命令行参数
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);//创建环境配置参数web/standard,设置环境监听器
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);//打印banner图,即spring boot启动时的图
context = this.createApplicationContext();//创建应用程序上下文
//准备异常报告器
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);//准备上下文环境 1监听配置 2设置环境参数 3初始化操作 4load对象的资源 加载启动类
this.refreshContext(context);//refresh调用过程和spring一样(AbstractApplicationContext),自动装配,tomcat配置
this.afterRefresh(context, applicationArguments);//留给用户扩展
stopWatch.stop();//计时器停止
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);//发布应用上下文启动,调用Runners执行器,发布上下文就绪时间,springboot启动完成
} catch (Throwable var10) {
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
}
}
springboot包和springboot autoconfig 包下游spring.factories配置文件
springboot autoconfig下的spring.factories里这个配置包含了自动注解
org.springframework.boot.autoconfigure.EnableAutoConfiguration