Spring Boot源码(一)
今天又是新的一天,时间过的真快,我真(心)慌(慌)。加油,小菜🦆,🦆 !
先大致看一下,spring boot 启动到底都干了什么吧
- 入口
// 启动入口方法
public static void main(String[] args) {
SpringApplication.run(IchatApplication.class, args);
}
- 进入run()方法
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
👇 👇 👇 👇 👇 👇 👇 👇
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
- 先看一下new SpringApplication(primarySources)
public SpringApplication(Class<?>... primarySources) {
this((ResourceLoader)null, primarySources);
}
👇 👇 👇 👇 👇 👇 👇 👇
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
// 保存主配置类
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
// 设置应用类型 NONE, SERVLET, REACTIVE;三种类型
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 设置初始化器 从类路径下找到 META/INF/Spring.factories 配置的所有 ApplicationContextInitializer,然后保存起来
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 设置监听器 从类路径下找到 META/INF/Spring.factories 配置的所有 ApplicationListener,然后保存起来
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
// 从多个配置类中找到有 main 方法的主配置类
this.mainApplicationClass = this.deduceMainApplicationClass();
}
primarySources:

- 重点来了 run()方法
public ConfigurableApplicationContext run(String... args) {
// 计时
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 什么IOC容器
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
// 从类路径下找到 META/INF/Spring.factories 获取 SpringApplicationRunListeners 并启动监听器
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 准备环境,包括创建环境,创建环境完成后回调 SpringApplicationRunListeners#environmentPrepared()方法,表示环境准备完成
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
// 创建 IOC 容器(决定创建 web 的 IOC 容器还是普通的 IOC 容器)
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
// Spring容器前置处理
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 刷新容器,IOC 容器初始化(如果是 Web 应用还会创建嵌入式的 Tomcat),扫描、创建、加载所有组件的地方
this.refreshContext(context);
// Spring容器后置处理
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
// 发出结束执行的事件
listeners.started(context);
// 执行Runners
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
❀❀ (ง •_•)ง little little 🦆🦆 ❀❀❀❀ ♕♕♕♕♕

浙公网安备 33010602011771号