spring boot 启动过程步骤详解
在外面启动spring boot项目的时候,入口在如下方法中

那么它的启动过程是如何的,通过点击进去发现是如下方法;
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
在这个方法中,可以分为2个大步骤;
一:new SpringApplication(primarySources): 即将我们传进来的class进行创建为springApplication。
在这里面
1.记录传入的primarySources
2.推测当前应用的类型

3.从spring.factories中获取BootstrapRegistryInitializer
4.从spring.factories中获取ApplicationContextInitializer
5.并且也获取ApplicationListener
this.bootstrapRegistryInitializers = new ArrayList<>( getSpringFactoriesInstances(BootstrapRegistryInitializer.class)); setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass();
6.推测当前main方法对应的启动类

二:调用run()方法
1.创建并初始化DefaultBootstrapContext
2.获取SpringApplicationRunListeners
SpringApplicationRunListeners listeners = getRunListeners(args);
3.调用SpringApplicationRunListeners 的starting(),发布ApplicationStartingEvent事件。
4.将启动参数artgs封装为ApplicationArguments对象
5.准备Environment
6.创建spring容器(ApplicationContext)
7.预处理spring容器
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
8.刷新
refreshContext(context);
9.调用SpringApplicationRunListener的started()方法,发布ApplicationStartedEvent事件
listeners.started(context, timeTakenToStartup);
10.调用SpringApplicationRunListener的ready()方法,发布ApplicationReadyEvent事件
try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
listeners.ready(context, timeTakenToReady);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}到此这篇关于spring boot 启动过程的文章就介绍到这了,更多相关spring boot 启动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决Callable的对象中,用@Autowired注入别的对象失败问题
这篇文章主要介绍了解决Callable的对象中,用@Autowired注入别的对象失败问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-07-07
springboot static关键字真能提高Bean的优先级(厉害了)
这篇文章主要介绍了springboot static关键字真能提高Bean的优先级(厉害了),需要的朋友可以参考下2020-07-07


最新评论