一、在springboot-autoconfigure的jar包下查找嵌入式的Servlet容器自动配置类
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.class
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication
@Import(BeanPostProcessorsRegistrar.class)
public class EmbeddedServletContainerAutoConfiguration {
/**
* Nested configuration if Tomcat is being used.
*/
@Configuration
//判断当前是否引入了Tomcat依赖;
@ConditionalOnClass({ Servlet.class, Tomcat.class })
//判断当前容器没有用户自己定义EmbeddedServletContainerFactory:嵌入式的Servlet容器工厂;作用:创建嵌入式的Servlet容器
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
public static class EmbeddedTomcat {
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory();
}
}
二、EmbeddedServletContainerFactory(嵌入式Servlet容器工厂),代码如下,三个实现类
三、EmbeddedServletContainer:(嵌入式的Servlet容器)
四、以TomcatEmbeddedServletContainerFactory为例
@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
ServletContextInitializer... initializers) {
//创建一个Tomcat
Tomcat tomcat = new Tomcat();
//配置Tomcat的基本参数
File baseDir = (this.baseDirectory != null ? this.baseDirectory
: createTempDir("tomcat"));
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
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);
//将配置好的Tomcat传入进去,返回一个EmbeddedServletContainer;并且启动Tomcat服务器
return getTomcatEmbeddedServletContainer(tomcat);
}
五、我们对嵌入式容器的配置修改是怎么生效?主要是通过在服务器自动配置里引入后置处理器注册组件BeanPostProcessorsRegistrar
BeanPostProcessorsRegistrar这个后置处理器注册组件主要是导入了一个EmbeddedServletContainerCustomizerBeanPostProcessor后置处理器,如下图
接下来我们看看这个EmbeddedServletContainerCustomizerBeanPostProcessor后置处理器干了些什么?
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof ConfigurableEmbeddedServletContainer) {
postProcessBeforeInitialization((ConfigurableEmbeddedServletContainer) bean);
}
return bean;
}
private void postProcessBeforeInitialization(
ConfigurableEmbeddedServletContainer bean) {
for (EmbeddedServletContainerCustomizer customizer : getCustomizers()) {
customizer.customize(bean);
}
}
private Collection
if (this.customizers == null) {
// Look up does not include the parent context
this.customizers = new ArrayList
this.beanFactory
.getBeansOfType(EmbeddedServletContainerCustomizer.class,
false, false)
.values());
Collections.sort(this.customizers, AnnotationAwareOrderComparator.INSTANCE);
this.customizers = Collections.unmodifiableList(this.customizers);
}
return this.customizers;
}
从上面postProcessBeforeInitialization方法可以看到EmbeddedServletContainerCustomizerBeanPostProcessor后置处理器主要用来处理ConfigurableEmbeddedServletContainer实例对象,而getCustomizers方法则告诉我们,从ioc容器中取出所有的EmbeddedServletContainerCustomizer实例,通过customizer.customize(bean)批处理容器配置的修改。而ServerProperties也实现了EmbeddedServletContainerCustomizer,所以他们都可以修改配置文件。