Spring Boot启动流程断点过程解析
2020-02-13 11:33
1196 查看
这篇文章主要介绍了Spring Boot启动流程断点过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
启动入口
跟进run方法 : 一个用来使用默认的配置从特定的源运行SpringApplication的静态帮助类。
这个类有两个重载方法,另一个用来传入多个源。通常,单个参数方法是数组方法的一个特例
创建一个新的SpringApplication实例。这个应用程序上下文会从特定的源加载Beans,这个实例会在调用run方法之前被定制化。
Web应用程序类型的枚举:WebApplicationType,包含NONE(不是web应用),SERVLET(基于Servlet的web应用),REACTIVE(基于Reactive的web应用)
- 直接jar包运行不使用web容器
- 使用嵌入式的Servlet web容器
- 使用反应式的web容器
setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class));
用于创建和加载Spring工厂方法实例
4.运行SpringApplication的run方法
Java SPI在 Spring Boot中的应用
SpringBoot底层的自动化都是由这些SPI实现类来实现的:初始化,监听器,自动配置导入监听器,自动配置导入过滤器,自动配置,失败分析器,可用模板提供者
Spring Boot找到main方式的方式
通过抛异常的形式来获取堆栈信息,再获取启动类的信息。
以上都是new SpringBootApplication的过程,下面分析run方法
/** * Run the Spring application, creating and refreshing a new * {@link ApplicationContext}. * 运行一个Spring应用,创建和刷新一个新的ApplicationContext * @param args the application arguments (usually passed from a Java main method) * 应用参数通过java main方法传递过来 * @return a running {@link ApplicationContext} */ public ConfigurableApplicationContext run(String... args) { // 任务计时器工具,可同时计数多个任务 StopWatch stopWatch = new StopWatch(); stopWatch.start(); //ApplicationContext是Spring的中心接口,为应用提供配置:1bean工厂2加载资源3注册的监听器发布事件4解析消息 ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); //headless 模式:服务器端模式,表示系统没有键盘鼠标等前端应用 configureHeadlessProperty(); //监听器容器,对run方法各个阶段事件进行监听,观察者模式 SpringApplicationRunListeners listeners = getRunListeners(args); //监听相应的事件,SpringApplicationEvent下的一个实现 listeners.starting(); try { //提供了对于运行SpringApplication参数的访问 ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); //环境配置:是servlet,reactive或者java应用环境,触发evn准备好的事件 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; }
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- WinCE 5.0 内核启动过程详细流程解析
- Android系统启动流程(一)解析init进程启动过程
- Activity启动过程全解析 <<主要流程说的并不完整,只有一半,有张流程图可以参考
- Spring Boot启动过程全面解析(三)
- Android系统启动流程(一)解析init进程启动过程
- Android系统启动流程(一)解析init进程启动过程
- Android系统启动流程(二)解析Zygote进程启动过程
- Android系统启动流程(三)解析SyetemServer进程启动过程
- android源码解析之(十四)-->Activity启动流程
- Spring boot 启动过程解析 logback
- Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(上)
- Oracle - 解析启动的全过程
- 老李推荐:第8章2节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-解析处理命令行参数
- Spring Boot使用过滤器Filter过程解析
- 8.0源码解析:Activity启动流程
- 软件外包项目管理的全流程全过程解析
- ARM linux解析之压缩内核zImage的启动过程 三
- SpringBoot启动流程分析(四):IoC容器的初始化过程
- 解析STM32启动过程