美文网首页
SpringBoot自动选择Tomcat、Jetty、under

SpringBoot自动选择Tomcat、Jetty、under

作者: AC编程 | 来源:发表于2022-11-16 10:18 被阅读0次

    一、maven依赖包

      <dependencies>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.3.23</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.3.23</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.3.23</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    

    二、SpringBoot选择web服务器说明

    以前我们启动一个普通web项目或者SpringMVC项目,需要将这些项目先部署到Tomcat(或Jetty),手动启动Tomcat才能访问到项目。现在用SpringBoot项目则省去了手动部署到Tomcat的环节,原因是SpringBoot内嵌了Tomcat,启动SpringBoot项目时,自动启动了内嵌的Tomcat。

    1、如果要将Tomcat换成Jetty或undertow,只需要将Tomcat的依赖包排除掉,换成Jetty或undertow的依赖包。

    2、Tomcat、Jetty、undertow三个依赖包,只能存在一个,如果同时存在多个,项目启动会失败,因为SpringBoot程序无法确定使用哪个web服务器。

    三、源码分析

    3.1 创建web服务器

    关键方法:createWebServer()

    createWebServer
    3.2 ServletWebServerFactory工厂

    ServletWebServerFactory工厂有Tomcat、Jetty、undertow三个实现子类,那SpringBoot到底选哪个子类呢?选择逻辑在getWebServerFactory()方法里实现

    ServletWebServerFactory工厂
    3.3 getWebServerFactory()方法

    通过该方法实现方式我们可以看到,Tomcat、Jetty、undertow只能存在一个,一个等于0或者多于1个,都会抛出异常。

    getWebServerFactory()
    3.4 Tomcat、Jetty、undertow三个实现子类的Bean

    关键字:EmbeddedTomcat
    类:ServletWebServerFactoryConfiguration

    ServletWebServerFactoryConfiguration

    3.5 TomcatServletWebServerFactory读取配件文件设置项目端口

    我们从上一步配置TomcatServletWebServerFactory时,并没有看到设置项目端口的代码,我们都知道写SpringBoot项目时,当我们不配置项目端口时,项目默认端口就是8080,如果我们需要更改端口,只需要在配置文件中配置server.port=8081,那这个配置文件是什么时候读取并进行设置的?SpringBoot是利用bean的后置处理器实现的。

    1

    ServletWebServerFactoryAutoConfiguration


    ServletWebServerFactoryAutoConfiguration

    TomcatServletWebServerFactoryCustomizer


    TomcatServletWebServerFactoryCustomizer

    ServerProperties配置类,读取server开头的配置项


    ServerProperties

    ServletWebServerFactoryCustomizer


    ServletWebServerFactoryCustomizer

    相关文章

      网友评论

          本文标题:SpringBoot自动选择Tomcat、Jetty、under

          本文链接:https://www.haomeiwen.com/subject/fmmsxdtx.html