目录
- 概述
- 技巧
- spring boot 如何启动应用程序
- run方法里面核心逻辑 SpringApplicaiton.run(xxx.class,args)
- 结束
概述
SpringBoot核心源码-启动类源码分析
技巧
如何给外部源码加注释,想要在源码中添加自己的注释,会弹出 file is read only
,代码只读
- 方式一:下载框架源码,安装对应的环境,编译,然后即可注释修改保存
- 方式二:idea插件
private notes
,不影响调试,方便简单
spring boot 如何启动应用程序
run方法里面核心逻辑 SpringApplicaiton.run(xxx.class,args)
# org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...)
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {this.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");# 存储项目启动类this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));// 判断应用是servlet还是reactive应用this.webApplicationType = WebApplicationType.deduceFromClasspath();// 从spring.factories中读取key为Bootstrapthis.bootstrapRegistryInitializers = new ArrayList<>(getSpringFactoriesInstances(BootstrapRegistryInitializer.class));// 设置初始化器 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));// 设置监听器setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));// 推断并配置项目main函数的主程序启动类this.mainApplicationClass = deduceMainApplicationClass();
}
public ConfigurableApplicationContext run(String... args) {Startup startup = Startup.create();if (this.registerShutdownHook) {SpringApplication.shutdownHook.enableShutdownHookAddition();}DefaultBootstrapContext bootstrapContext = createBootstrapContext();ConfigurableApplicationContext context = null;configureHeadlessProperty();SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);Banner printedBanner = printBanner(environment);context = createApplicationContext();context.setApplicationStartup(this.applicationStartup);prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);refreshContext(context);afterRefresh(context, applicationArguments);startup.started();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), startup);}listeners.started(context, startup.timeTakenToStarted());callRunners(context, applicationArguments);}catch (Throwable ex) {throw handleRunFailure(context, ex, listeners);}try {if (context.isRunning()) {listeners.ready(context, startup.ready());}}catch (Throwable ex) {throw handleRunFailure(context, ex, null);}return context;
}