嵌入式Servlet容器简单分析

1、切换嵌入式Servlet容器

  • 默认支持的webServer
    • TomcatJetty, or Undertow
    • ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器
  • 切换服务器
    • 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-tomcat</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      复制代码
      • 原理
        • SpringBoot应用启动发现当前是Web应用。web场景包-导入tomcat
        • web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext 
        • ServletWebServerApplicationContext  启动的时候寻找 ServletWebServerFactory(Servlet 的web服务器工厂---> Servlet 的web服务器) 
        • SpringBoot底层默认有很多的WebServer工厂;TomcatServletWebServerFactoryJettyServletWebServerFactory, or UndertowServletWebServerFactory
        • 底层直接会有一个自动配置类。ServletWebServerFactoryAutoConfiguration
        • ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)
        • ServletWebServerFactoryConfiguration 配置类 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
        • TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize---this.tomcat.start();
        • 内嵌服务器,就是手动把启动服务器的代码调用(tomcat核心jar包存在)

      2、定制Servlet容器

      • 实现 WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
        • 把配置文件的值和ServletWebServerFactory 进行绑定
      • 修改配置文件 server.xxx
      • 直接自定义 ConfigurableServletWebServerFactory

       

      xxxxxCustomizer:定制化器,可以改变xxxx的默认规则

      复制代码
      import org.springframework.boot.web.server.WebServerFactoryCustomizer;
      import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
      import org.springframework.stereotype.Component;
      
      @Component
      public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
      
          @Override
          public void customize(ConfigurableServletWebServerFactory server) {
              server.setPort(9000);
          }
      
      }
      复制代码

      搜一会又有很多xxxFactoryCustomizer 

       

       然后实现接口 WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>  进入ConfigurableJettyWebServerFactory

       

       又继承了ConfigurableWebServerFactory 进入后

       

       

      这个就是默认的实现。
      自己实现
      public class jettyWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>{
        然后重写customize 方法
      }
      public class jettywebserverfactorycustomizer implements WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> {
          @Override
          public void customize(ConfigurableJettyWebServerFactory factory) {
              factory.setPort(80);
          }
      }

       

posted @   咖喱给给啊  阅读(34)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示