springboot web - 启动(2) run()

网友投稿 242 2022-08-04

springboot web - 启动(2) run()

在创建 SpringApplication 之后, 调用了 run() 方法.

configureHeadlessProperty()

这里会默认设置为 true.

getRunListeners()

private SpringApplicationRunListeners getRunListeners(String[] args) {

Class>[] types = new Class>[] { SpringApplication.class, String[].class };

return new SpringApplicationRunListeners(logger, getSpringFactoriesInstances(

SpringApplicationRunListener.class, types, this, args));

}

这里是获取配置的监听器, 并封装到  SpringApplicationRunListeners 了中的 this.liseners 属性中.

这里获取到的是: org.springframework.boot.context.event.EventPublishingRunListener

listeners 这块内容比较多, 放在后面去详细解析

prepareEnvironment()

configureIgnoreBeanInfo()

默认设置 spring.beaninfo.ignore 为 true

printBanner()

这里是打印 banner ,  在 application.yml 中, 加入配置

spring:

main:

banner-mode: "off"

可以关闭 banner 打印, 当然这里也可以自定义自己的 banner . 可有可无的功能, 不去管它. 打印就打印吧.

createApplicationContext()

根据环境类型, 创建 ApplicationContext 对象 :  AnnotationConfigServletWebServerApplicationContext

prepareContext()

refreshContext()

这里执行的内容非常多, 不在此解析了.

afterRefresh()

protected void afterRefresh(ConfigurableApplicationContext context,

ApplicationArguments args) {

}

这里是一个空方法, 可以留个子类重写, 进行扩展操作

callRunners()

执行到这里是, 容器里面还没有哪两个类, 所以 runners.size() = 0. 没有执行什么逻辑.

总结:

run() 方法主要进行了以下几个操作

1. 获取并启动了监听器, 发布了 容器启动事件(ApplicationStartingEvent )

2. 构造容器环境

3. 创建容器

4. 实例化 SpringBootExceptionReporter , 用来支持报告关于启动的错误

5. 准备容器

6. 刷新容器

7. 执行刷新完容器的操作(暂时为空操作)

8. 发布了 容器启动完成事件(ApplicationStartedEvent)

9. 发布了 容器已准备好事件(ApplicationReadyEvent), 如果执行失败, 则会发布容器失败事件(ApplicationFailedEvent)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot web - 启动(1) 创建SpringApplication(springboot的优点)
下一篇:srpingboot web - 启动(3) 监听器(spring boot是干嘛的)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~