springboot对接tomcat

调用的地方在 ServletWebServerApplicationContext 里.

创建 webServer

getWebServer:166, TomcatServletWebServerFactory (org.springframework.boot.web.embedded.tomcat)
createWebServer:181, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
onRefresh:154, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:543, AbstractApplicationContext (org.springframework.context.support)
refresh:142, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:775, SpringApplication (org.springframework.boot)
refreshContext:397, SpringApplication (org.springframework.boot)
run:316, SpringApplication (org.springframework.boot)
main:16, ConditionalApplication (spring.study.conditional)

EmbeddedTomcat

1
2
3
4
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
return new TomcatServletWebServerFactory(); // 创建 tomcat 工厂类,并返回注册到 spring 中
}

TomcatServletWebServerFactory

调用 tomcat 的 api,组装 tomcat.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory
: createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol); // 创建 connector
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine()); // 定制 engine
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatWebServer(tomcat);
}

启动 webServer

startWebServer:309, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
finishRefresh:164, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:552, AbstractApplicationContext (org.springframework.context.support)
refresh:142, ServletWebServerApplicationContext (org.springframework.boot.web.servlet.context)
refresh:775, SpringApplication (org.springframework.boot)
refreshContext:397, SpringApplication (org.springframework.boot)
run:316, SpringApplication (org.springframework.boot)
main:16, ConditionalApplication (spring.study.conditional)