24 Servlet容器配置原理

1 SpringBoot支持的容器

Tomcat:
	默认使用
	
Jetty:
	长连接,eg:聊天系统

Undertow:
	不支持jsp
	高性能,非阻塞

2 将默认容器设为jetty


<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.0modelVersion>

    <groupId>com.springbootgroupId>
    <artifactId>springboot-21artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>springboot-21name>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.0.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>

            
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

24 Servlet容器配置原理_第1张图片

3 嵌入式Servlet容器自动配置原理

24 Servlet容器配置原理_第2张图片

@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,调用定制器的定制方法

你可能感兴趣的:(SpringBoot)