获取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是哪里的?
- 在我们自动配置类ServletWebServerFactoryAutoConfiguration中定义 。
推荐阅读
-
vivox70和x60对比_vivox70和x60哪个好
-
-
-
-
-
-
2023中山市三角四海学校招生简章 2023中山市三角四海学校招生简章公告
-
-
食用油标准号q和gb区别0024S 食用油标准号q和gb区别
-
-
-
-
-
-
-
-
07年大众帕萨特是国几的 国家阶段机动车污染物排放标准的意义
-
2023年属兔郝姓男孩取什么名字好 代表祥瑞的男宝宝名
-
-