嵌入tomcat源码分析
在启动spring boot
工程时利用@SpringBootApplication
注解,该注解启动@EnableAutoConfiguration
自动配置,加载META-INF/spring.factories
文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
...
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
其中EmbeddedServletContainerAutoConfiguration
被加载
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(BeanPostProcessorsRegistrar.class)
public class EmbeddedServletContainerAutoConfiguration {
...
}
@ConditionalOnWebApplication
注解表明只有在web
环境下才会创建容器相关信息,因此应用无需容器则使用
new SpringApplicationBuilder(Xxx.class).web(false).run(args)
实现。
TomcatEmbeddedServletContainerFactory.java
@Configuration
@ConditionalOnClass({ Servlet.class, Tomcat.class })
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory();
}
}
优先创建TomcatEmbeddedServletContainerFactory
,由于存在@ConditionalOnMissingBean
因此优先使用用户自定义的EmbeddedServletContainerFactory
,
此时创建了工厂,但是tomcat
是如何启动的呢?
在spring boot
中常使用的上下文为AnnotationConfigEmbeddedWebApplicationContext
,通过前面的文章也知道加载BeanDefinition
是在
AbstractApplicationContext#refresh()
方法中,具体详细的实现可以参考这里1 2,有阅读源码的备注信息.
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); //为BeanFactory设置后处理器,用依拓展
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); //执行BeanFactoryPostProcessor,因此在执行BeanFactoryPostProcessor子类时,bean是没有被实例化的
// 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(); //释放缓存
}
}
}
其中的onRefresh();
交由子类实现EmbeddedWebApplicationContext
(public class AnnotationConfigEmbeddedWebApplicationContext
)
extends EmbeddedWebApplicationContext
@Override
protected void onRefresh() {
super.onRefresh();
try {
createEmbeddedServletContainer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start embedded container",
ex);
}
}
调用方法
/**
* 创建内嵌容器
*/
private void createEmbeddedServletContainer() {
EmbeddedServletContainer localContainer = this.embeddedServletContainer;
ServletContext localServletContext = getServletContext();
if (localContainer == null && localServletContext == null) {
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory(); //获取自动加载的工厂
this.embeddedServletContainer = containerFactory
.getEmbeddedServletContainer(getSelfInitializer());
}
else if (localServletContext != null) {
try {
getSelfInitializer().onStartup(localServletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context",
ex);
}
}
initPropertySources();
}
getEmbeddedServletContainerFactory
获取容器工厂,通过
containerFactory.getEmbeddedServletContainer(getSelfInitializer());
创建一个内建容器,这里的getSelfInitializer()
返回一个ServletContextInitializer
,其实现为
return new ServletContextInitializer() {
@Override
prepareContext(tomcat.getHost(), initializers); public void onStartup(ServletContext servletContext) throws ServletException {
selfInitialize(servletContext);
}
}; customizer.customize(bean)
其中的selfInitialize(servletContext);
等后续再回过来看,这里很关键。
继续看containerFactory.getEmbeddedServletContainer(getSelfInitializer());
方法,默认调用TomcatEmbeddedServletContainerFactory
中的方法:
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
Tomcat tomcat = new</