24 Servlet容器配置原理
阿新 • • 發佈:2018-12-29
1 SpringBoot支援的容器
Tomcat:
預設使用
Jetty:
長連線,eg:聊天系統
Undertow:
不支援jsp
高效能,非阻塞
2 將預設容器設為jetty
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot</groupId>
<artifactId>springboot-21</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-21</name>
<description >Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除內嵌的tomcat-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入其他的Servlet容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3 嵌入式Servlet容器自動配置原理
@Configuration
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
// BeanPostProcessorsRegistrar(後置處理器):向容器中匯入元件(Spring註解版)
// 匯入了WebServerFactoryCustomizerBeanPostProcessor
// 後置處理器:bean初始化前後(建立完物件,還沒賦值賦值)執行初始化工作
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
...
}
- EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class
@Configuration
class ServletWebServerFactoryConfiguration {
ServletWebServerFactoryConfiguration() {
}
@Configuration
@ConditionalOnClass({Servlet.class, Undertow.class, SslClientAuthMode.class})
@ConditionalOnMissingBean(
value = {ServletWebServerFactory.class},
search = SearchStrategy.CURRENT
)
public static class EmbeddedUndertow {
public EmbeddedUndertow() {
}
@Bean
public UndertowServletWebServerFactory undertowServletWebServerFactory() {
return new UndertowServletWebServerFactory();
}
}
@Configuration
@ConditionalOnClass({Servlet.class, Server.class, Loader.class, WebAppContext.class})
@ConditionalOnMissingBean(
value = {ServletWebServerFactory.class},
search = SearchStrategy.CURRENT
)
public static class EmbeddedJetty {
public EmbeddedJetty() {
}
@Bean
public JettyServletWebServerFactory JettyServletWebServerFactory() {
return new JettyServletWebServerFactory();
}
}
@Configuration
// 判斷當前是否引入了Tomcat依賴(pom.xml)
@ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
// 判斷當前容器是否有使用者自己定義ServletWebServerFactory:嵌入式的Servlet容器工廠;
// 作用:建立嵌入式的Servlet容器
@ConditionalOnMissingBean(
value = {ServletWebServerFactory.class},
search = SearchStrategy.CURRENT
)
public static class EmbeddedTomcat {
public EmbeddedTomcat() {
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
}
}
- TomcatServletWebServerFactory
public WebServer getWebServer(ServletContextInitializer... initializers) {
// 建立一個Tomcat
Tomcat tomcat = new Tomcat();
// 配置Tomcat
File baseDir = this.baseDirectory != null ? this.baseDirectory : this.createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
this.customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
this.configureEngine(tomcat.getEngine());
Iterator var5 = this.additionalTomcatConnectors.iterator();
while(var5.hasNext()) {
Connector additionalConnector = (Connector)var5.next();
tomcat.getService().addConnector(additionalConnector);
}
this.prepareContext(tomcat.getHost(), initializers);
// 將配置完成的Tomcat傳入,返回TomcatWebServer,啟動Tomcat伺服器
return this.getTomcatWebServer(tomcat);
}
4 嵌入式容器的配置修改原理
4.1 通過application.properties修改
- org.springframework.boot.autoconfigure.web.ServerProperties
4.2 通過WebServerFactoryCustomizer修改
- org.springframework.boot.web.server.ConfigurableWebServerFactory
5 BeanPostProcessorsRegistrar(後置處理器)
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (this.beanFactory != null) {
this.registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor", WebServerFactoryCustomizerBeanPostProcessor.class);
this.registerSyntheticBeanIfMissing(registry, "errorPageRegistrarBeanPostProcessor", ErrorPageRegistrarBeanPostProcessor.class);
}
}
- WebServerFactoryCustomizerBeanPostProcessor
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//如果初始化的是WebServerFactory型別的元件
if (bean instanceof WebServerFactory) {
this.postProcessBeforeInitialization((WebServerFactory)bean);
}
return bean;
}
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
// 獲取所有的定製器,呼叫每個定製器的customize方法來給Servlet容器進行屬性賦值;
((Callbacks)LambdaSafe.callbacks(WebServerFactoryCustomizer.class, this.getCustomizers(), webServerFactory, new Object[0]).withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)).invoke((customizer) -> {
customizer.customize(webServerFactory);
});
}
private void postProcessBeforeInitialization(
ConfigurableEmbeddedServletContainer bean) {
//獲取所有的定製器,呼叫每一個定製器的customize方法來給Servlet容器進行屬性賦值;
for (EmbeddedServletContainerCustomizer customizer : getCustomizers()) {
customizer.customize(bean);
}
}
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
if (this.customizers == null) {
this.customizers = new ArrayList(this.getWebServerFactoryCustomizerBeans());
this.customizers.sort(AnnotationAwareOrderComparator.INSTANCE);
this.customizers = Collections.unmodifiableList(this.customizers);
}
return this.customizers;
}
private Collection<WebServerFactoryCustomizer<?>> getWebServerFactoryCustomizerBeans() {
// 從容器中獲取所有這葛型別的元件:WebServerFactoryCustomizer
// 定製Servlet容器:給容器中可以新增一個WebServerFactoryCustomizer型別的元件
return this.beanFactory.getBeansOfType(WebServerFactoryCustomizer.class, false, false).values();
}
6 總結
1: 自動配置類生效ServletWebServerFactoryAutoConfiguration,根據依賴新增嵌入式容器工廠TomcatServletWebServerFactory
2: 容器中元件建立物件時,會呼叫後置處理器,WebServerFactoryCustomizerBeanPostProcessor
3: 後置處理器,從容器中獲取所有的WebServerFactoryCustomizer.class,呼叫定製器的定製方法