springboot如何启动web环境

  1. 当pom.xml里,不依赖spring-boot-starter-web时,

SpringApplication.run(App.class, args);

不会启动tomcat

  1. 当pom.xml里,依赖spring-boot-starter-web后,

SpringApplication.run(App.class, args);

会启动tomcat

  1. 为何?
    3.1 在springboot启动时,各种refresh会来到
    springboot源码系列 - springboot如何启动web环境
    applicationContext的值取决于webApplicationType, 那么webApplicationType的值是关键,webApplicationType在SpringApplication构造方法里被设置,一共有三个类型可选择(REACTIVE, SERVLET, NONE)

springboot源码系列 - springboot如何启动web环境

重点代码:
this.webApplicationType = WebApplicationType.deduceFromClasspath();
从Classpath里推断webapp的类型

springboot源码系列 - springboot如何启动web环境

这段代码的主要目的,是以下3点:
a. classpath里有webflux(pom里依赖了,就会扫描加载到),webApplicationType = REACTIVE;不在判断是否有servlet,webflux优先级很高
b. classpath里有servlet(pom里依赖了,就会扫描加载到),webApplicationType = SERVLET;
c. classpath里没有以上两个,webApplicationType = NONE;

搞定了webApplicationType,就可以来搞applicationContext了,

springboot源码系列 - springboot如何启动web环境

springboot源码系列 - springboot如何启动web环境

很显然,得到了DEFAULT_SERVLET_WEB_CONTEXT_CLASS作为context,也就是AnnotationConfigServletWebServerApplicationContext

所以呢,在后来的

springboot源码系列 - springboot如何启动web环境

这里的refresh就会进入

springboot源码系列 - springboot如何启动web环境

而且,super里的onRefresh()也会进入servletWebServletApplicationContext里

springboot源码系列 - springboot如何启动web环境

那么好,下面就是最关键,最核心的源码了

springboot源码系列 - springboot如何启动web环境

这里会创建,启动tomcat,具体我就不在跟进去了,自己看吧。

相关文章:

  • 2021-06-09
  • 2022-01-25
  • 2022-12-23
  • 2021-07-30
  • 2021-05-17
  • 2021-11-11
  • 2021-10-01
猜你喜欢
  • 2021-09-16
  • 2021-09-01
  • 2021-05-06
  • 2022-12-23
  • 2021-12-28
  • 2022-02-19
相关资源
相似解决方案