获取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中定义 。
推荐阅读
-
-
-
-
奥迪a5是cvt变速箱吗? 奥迪A5变速箱是什么牌子
-
南昌附近的油菜花基地 2023南昌县油菜花观赏地点推荐
-
-
毛豆可以和胡萝卜一起煮吗 毛豆不能和什么东西一起吃
-
-
-
-
-
-
-
-
8朵玫瑰花语每朵代表什么 玫瑰花语每朵代表什么意思
-
微信朋友圈评论怎么统一回复比较好 微信朋友圈评论怎么统一回复
-
-
-
-
国医大师朱良春养生经 每天一碗长寿粥 朱良春长寿粥