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

获取tomcat的配置

  • 自动配置类ServletWebServerFactoryAutoConfiguration上除了import三个web容器,还import了BeanPostProcessorsRegistrar 。
  • BeanPostProcessorsRegistrar实现了ImportBeanDefinitionRegistrar,所以他会在spring启动的时候调用registerBeanDefinitions方法 。
  • registerBeanDefinitions会注册一个Bean:webServerFactoryCustomizerBeanPostProcessor 。
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {    // Bean工厂 , 一个Aware回调进行赋值    if (this.beanFactory == null) {        return;    }    // 注册webServerFactoryCustomizerBeanPostProcessor这个Bean 。    registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor",        WebServerFactoryCustomizerBeanPostProcessor.class,        WebServerFactoryCustomizerBeanPostProcessor::new);    // 注册errorPageRegistrarBeanPostProcessor    registerSyntheticBeanIfMissing(registry, "errorPageRegistrarBeanPostProcessor",        ErrorPageRegistrarBeanPostProcessor.class, ErrorPageRegistrarBeanPostProcessor::new);}
  • webServerFactoryCustomizerBeanPostProcessor实现了BeanPostProcessor,所以他会在启动的时候调用postProcessBeforeInitialization方法 。
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是哪里的?