【Spring boot】整合tomcat底层原理( 四 )


private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {    // 找到WebServerFactoryCustomizer的Bean    LambdaSafe.callbacks(WebServerFactoryCustomizer.class, getCustomizers(), webServerFactory)        // 标记日志用的类        .withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)        // 调用customize方法,传入webServerFactory        .invoke((customizer) -> customizer.customize(webServerFactory));}

  • postProcessBeforeInitialization中会调用WebServerFactoryCustomizer类customize方法,在系统中的唯一实现:ServletWebServerFactoryCustomizer的customize方法 。
  • customize把配置中的内容设置到ConfigurableServletWebServerFactory对象中 。他的实现TomcatServletWebServerFactory在启动的时候就会有值!
@Overridepublic void customize(ConfigurableServletWebServerFactory factory) {    PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();    map.from(this.serverProperties::getPort).to(factory::setPort);    map.from(this.serverProperties::getAddress).to(factory::setAddress);    map.from(this.serverProperties.getServlet()::getContextPath).to(factory::setContextPath);    map.from(this.serverProperties.getServlet()::getApplicationDisplayName).to(factory::setDisplayName);    map.from(this.serverProperties.getServlet()::isRegisterDefaultServlet).to(factory::setRegisterDefaultServlet);    map.from(this.serverProperties.getServlet()::getSession).to(factory::setSession);    map.from(this.serverProperties::getSsl).to(factory::setSsl);    map.from(this.serverProperties.getServlet()::getJsp).to(factory::setJsp);    map.from(this.serverProperties::getCompression).to(factory::setCompression);    map.from(this.serverProperties::getHttp2).to(factory::setHttp2);    map.from(this.serverProperties::getServerHeader).to(factory::setServerHeader);    map.from(this.serverProperties.getServlet()::getContextParameters).to(factory::setInitParameters);    map.from(this.serverProperties.getShutdown()).to(factory::setShutdown);    for (WebListenerRegistrar registrar : this.webListenerRegistrars) {        registrar.register(factory);    }    if (!CollectionUtils.isEmpty(this.cookieSameSiteSuppliers)) {        factory.setCookieSameSiteSuppliers(this.cookieSameSiteSuppliers);    }}ServletWebServerFactoryCustomizer这个Bean是哪里的?
  • 在我们自动配置类ServletWebServerFactoryAutoConfiguration中定义 。
@Beanpublic ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties, ObjectProvider<WebListenerRegistrar> webListenerRegistrars, ObjectProvider<CookieSameSiteSupplier> cookieSameSiteSuppliers) {    return new ServletWebServerFactoryCustomizer(serverProperties,webListenerRegistrars.orderedStream().collect(Collectors.toList()),cookieSameSiteSuppliers.orderedStream().collect(Collectors.toList()));}【【Spring boot】整合tomcat底层原理】

经验总结扩展阅读