前言
Tomcat扩展:
概念
Tomcat
- Tomcat是怎么加载的
1、ServletWebServerFactoryAutoConfiguration
2、ServerProperties
1.1
//对Tomcat进行初始化
@ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat")
public TomcatServletWebServerFactoryCustomizer tomcatServletWebServerFactoryCustomizer
1.1.1
WebServerFactoryCustomizer
1.2
Import
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class
1.2.1
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
static class EmbeddedTomcat {}
1.2.2
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#getWebServer
是哪里调用的?
1.2.2.1
SpringBoot的启动流程
org.springframework.context.support.AbstractApplicationContext#onRefresh
1.2.2.2
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh
1.2.3
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#getTomcatWebServer
1.2.4
org.springframework.boot.web.embedded.tomcat.TomcatWebServer#TomcatWebServer(org.apache.catalina.startup.Tomcat, boolean, org.springframework.boot.web.server.Shutdown)
1.2.5
org.springframework.boot.web.embedded.tomcat.TomcatWebServer#initialize
- SpringBoot加载流程
ApplicationContextInitializer:Context初始化后的类
SpringApplicationRunListener:SpringBoot运行监听的的类,整个springboot的启动流程
启动后操作
ApplicationRunner
CommandLineRunner
1 新建SpringApplication对象,添加监听器
org.springframework.boot.SpringApplication#SpringApplication(org.springframework.core.io.ResourceLoader, java.lang.Class<?>...)
2 run
org.springframework.boot.SpringApplication#run(java.lang.String...)
3 加载 SpringApplicationRunListener
org.springframework.boot.SpringApplication#getRunListeners
4 准备环境,绑定到Spring上下文
org.springframework.boot.SpringApplication#prepareEnvironment
5 打印banner
org.springframework.boot.SpringApplication#printBanner
6 创建上下文 WebApplicationType Servlet
org.springframework.boot.ApplicationContextFactory#create
7 准备上下文
org.springframework.boot.SpringApplication#prepareContext
7.1
org.springframework.boot.SpringApplicationRunListeners#contextPrepared
7.2
org.springframework.boot.SpringApplicationRunListeners#contextLoaded
8 注册shutdown Hook 刷新上下文 接轨spring
org.springframework.boot.SpringApplication#refreshContext
9 运行ApplicationRunner CommandLineRunner
org.springframework.boot.SpringApplication#callRunners
10 执行running
org.springframework.boot.SpringApplicationRunListeners#running
- spring.factories
org.springframework.context.ApplicationContextInitializer=\
com.today.roc.springboot.running.RocApplicationContextInitializer
org.springframework.boot.SpringApplicationRunListener=\
com.today.roc.springboot.running.RocSpringApplicationRunListerner