Servlet容器:管理、运行Servlet组件(Servlet、Filter、Listener)的环境,一般指服务器
1. 自动配置原理
- SpringBoot默认嵌入Tomcat作为Servlet容器
- 自动配置类是ServletWebServerFactoryAutoConfiguration、EmbeddedWebServerFactoryCustomizerAutoConfiguration
- 从自动配置类开始分析功能。如
xxxxAutoConfiguration
先分析ServletWebServerFactoryAutoConfiguration自动配置类:
......省略部分......
@AutoConfiguration(
after = {SslAutoConfiguration.class}
)
@AutoConfigureOrder(Integer.MIN_VALUE)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class, ServletWebServerFactoryConfiguration.EmbeddedJetty.class, ServletWebServerFactoryConfiguration.EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
......省略部分......
}
- ServletWebServerFactoryAutoConfiguration自动配置了嵌入式容器场景
- 绑定了ServerProperties配置类,所有和服务器有关的配置都是
server
开头,和tomcat有关的配置都是server.tomcat
开头 - ServletWebServerFactoryAutoConfiguration导入了嵌入式的三大服务器 Tomcat、Jetty、Undertow
1.导入Tomcat、Jetty、Undertow都有条件注解。系统中有这个类才行,也就是导了这个包
2.默认 Tomcat配置生效。给容器中放了TomcatServletWebServerFactory,这是一个 web服务器工厂,造web服务器的
......省略部分......
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
@ConditionalOnMissingBean(
value = {ServletWebServerFactory.class},
search = SearchStrategy.CURRENT
)
static class EmbeddedTomcat {
EmbeddedTomcat() {
}
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory(ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers, ObjectProvider<TomcatContextCustomizer> contextCustomizers, ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers) {
......省略部分......
}
......省略部分......
}
......省略部分......
3.web服务器工厂 都有一个功能,getWebServer获取web服务器
4.TomcatServletWebServerFactory创建了tomcat
......省略部分......
public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory implements ConfigurableTomcatWebServerFactory, ResourceLoaderAware {
......省略部分......
public WebServer getWebServer(ServletContextInitializer... initializers) {
......省略部分......
Tomcat tomcat = new Tomcat();
......省略部分......
}
......省略部分......
}
- ServletWebServerApplicationContextioc容器,启动的时候createWebServer方法会调用TomcatServletWebServerFactory.getWebServer创建web服务器
- Spring容器刷新(启动)的时候,执行经典的十二大步,会预留一个时机,调用onRefresh()刷新子容器
......省略部分......
public class ServletWebServerApplicationContext extends GenericWebApplicationContext
implements ConfigurableWebServerApplicationContext {
......省略部分......
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
......省略部分......
}
- 经典的十二大步调用
......省略部分......
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
......省略部分......
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
......省略部分......
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// 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();
}
......省略部分......
}
......省略部分......
}
总结:Web场景的Spring容器启动,在onRefresh的时候,会调用创建web服务器的方法。Web服务器的创建是通过WebServerFactory搞定的。容器中又会根据导人员的包进行条件注解,启动相关的服务器配置,默认EmbeddedTomcat会给容器中放一个TomcatServletWebServerFactory,进行项目启动,自动创建出Tomcat
2. 自定义嵌入式容器
切换嵌入式服务器。如下所示:
<properties>
<!-- springboot3使用jakarta-servlet 6,但是jetty使用jakarta-servlet 5- -->
<jakarta-servlet.version>5.0.0</jakarta-servlet.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
3. 最佳实践
用法:
- 修改server下的相关配置就可以修改服务器参数
- 通过给容器中放一个ServletWebServerFactory,来禁用掉SpringBoot默认放的服务器工厂,实现自定义嵌入任意服务器