花了一個(gè)禮拜的時(shí)間赊窥,大致走讀了一下springboot內(nèi)嵌tomcat的代碼逝钥,為下一步自己實(shí)現(xiàn)一個(gè)web容器做知識(shí)儲(chǔ)備隘弊。這里對(duì)走讀的代碼做一個(gè)大致的記錄乞而。
springboot內(nèi)嵌tomcat
我們知道送悔,springboot自動(dòng)裝配的相關(guān)功能封裝在spring-boot-autoconfigure包中。這里通過(guò)走讀spring-boot的2.4.0-M版本的代碼來(lái)大致看一下tomcat的啟動(dòng)過(guò)程爪模。
首先欠啤,寫(xiě)一個(gè)簡(jiǎn)單的demo,調(diào)用springboot的run方法屋灌,一步一步來(lái)看tomcat如何啟動(dòng)的洁段。
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class);
}
}
查看run方法里做了什么。
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
//封裝參數(shù)
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//準(zhǔn)備springboot運(yùn)行相關(guān)的環(huán)境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
//確定spring的上下文信息共郭,這里是webservlet類型眉撵,通過(guò)反射得到AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class<?>[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//這里真正調(diào)到spring里,進(jìn)行bean的初始化
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;
}
這里落塑,初始化的context
是一個(gè)AnnotationConfigServletWebServerApplicationContext
。我們看一下該類的繼承關(guān)系罐韩。
本篇日記重點(diǎn)看的就是refreshContext(context)
憾赁,這個(gè)真正的進(jìn)入spring bean的處理。也在這個(gè)方法里散吵,進(jìn)行了tomcat的初始化及啟動(dòng)龙考。繼續(xù)走讀這個(gè)方法的代碼。一步一步跟下去矾睦,就進(jìn)入了AbstractApplicationContext
的refresh
方法中。方法的代碼如下:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
通過(guò)spring的注釋,可以大致猜測(cè)一膨,關(guān)于tomcat的初始化诞仓,應(yīng)該在方法onRefresh
中實(shí)現(xiàn)。當(dāng)然赁温,這里是通過(guò)debug源碼坛怪,看調(diào)用棧信息能確切的知道這個(gè)方法就是tomcat初始化的入口淤齐。這個(gè)方法實(shí)現(xiàn)在子類里。上面在創(chuàng)建spring的context
時(shí)袜匿,我們看到更啄,這里創(chuàng)建的是一個(gè)ServletWebServerApplicationContext
,onRefresh
就在該類中實(shí)現(xiàn)居灯。進(jìn)入該方法祭务,看到如下代碼:
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
createWebServer
就是創(chuàng)建webserver的代碼。代碼:
private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
this.webServer = factory.getWebServer(getSelfInitializer());
getBeanFactory().registerSingleton("webServerGracefulShutdown",
new WebServerGracefulShutdownLifecycle(this.webServer));
getBeanFactory().registerSingleton("webServerStartStop",
new WebServerStartStopLifecycle(this, this.webServer));
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
ServletWebServerFactory
為自動(dòng)裝配進(jìn)來(lái)怪嫌,此時(shí)在BeanFactory
中已經(jīng)存在了义锥。具體代碼配置在spring-boot-autoconfigure包的META-INF/spring.factories中。
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
......
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
ServletWebServerFactoryAutoConfiguration
中import了一些類喇勋,其中impot了EmbeddedTomcat缨该。
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Servlet.class, Tomcat.class, UpgradeProtocol.class })
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
static class EmbeddedTomcat {
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory(
ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,
ObjectProvider<TomcatContextCustomizer> contextCustomizers,
ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.getTomcatConnectorCustomizers()
.addAll(connectorCustomizers.orderedStream().collect(Collectors.toList()));
factory.getTomcatContextCustomizers()
.addAll(contextCustomizers.orderedStream().collect(Collectors.toList()));
factory.getTomcatProtocolHandlerCustomizers()
.addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));
return factory;
}
}
在這里生成了一個(gè)TomcatServletWebServerFactory
的bean。
再次回到createWebServer
中川背,this.webServer = factory.getWebServer(getSelfInitializer());
從factory中生產(chǎn)出一個(gè)webserver贰拿。我們看這個(gè)webServer是如何生產(chǎn)出來(lái)的。
public WebServer getWebServer(ServletContextInitializer... initializers) {
if (this.disableMBeanRegistry) {
Registry.disableRegistry();
}
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
connector.setThrowOnFailure(true);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
for (Connector additionalConnector : this.additionalTomcatConnectors) {
tomcat.getService().addConnector(additionalConnector);
}
prepareContext(tomcat.getHost(), initializers);
return getTomcatWebServer(tomcat);
}
上面這段代碼熄云,就正式進(jìn)入到了tomcat的領(lǐng)域了膨更。為了能更好的閱讀代碼,我們需要了解tomcat的體系結(jié)構(gòu)缴允。這里通過(guò)代碼的閱讀和分析荚守,總結(jié)了一張結(jié)構(gòu)圖。
tomcat的初始化练般,無(wú)非就是將圖中的各個(gè)部分初始化矗漾,塞到對(duì)應(yīng)的位置。有了這張圖薄料,代碼讀起來(lái)就比較輕松了敞贡,代碼也相對(duì)簡(jiǎn)單,可以自己按照?qǐng)D中的結(jié)構(gòu)摄职,進(jìn)行走讀誊役。這里不再記錄。
getWebServer
代碼走完谷市,tomcat就初始化完成了蛔垢,那么是不是就可以接收請(qǐng)求了呢。當(dāng)然不行迫悠。socket還沒(méi)起來(lái)鹏漆,tomcat的工作線程還沒(méi)起來(lái)。這部分功能不在上面的onRefresh
中,而在接下來(lái)的finishRefresh
中甫男,只有所有的準(zhǔn)備工作都已經(jīng)完成了且改,請(qǐng)求才能接進(jìn)來(lái)。這部分功能將在下一篇日記中走讀板驳。至此又跛,springboot的內(nèi)嵌tomcat初始化完成。這里只記錄了主流程的初始化若治,其中一些配置類信息如
ServerProperties
等的初始化并沒(méi)有在這篇日記中體現(xiàn)慨蓝。