SpringBoot切换Tomcat容器

SpringBoot默认的容器为Tomcat,

依赖包在spring-boot-starter-web下

Xml代码 收藏代码

<dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-test</artifactId>  
            <scope>test</scope>  
        </dependency>  
  
</dependencies>  

img

SpringBoot把容器修改为Jetty

方法很简单,就是在pom.xml文件中,在引用的spring-boot-starter-web排除Tomcat的依赖包,然后再引入Jetty容器的依赖包,如下:

Xml代码 收藏代码

<dependencies>  
        <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>  
  
        <!-- Jetty适合长连接应用,就是聊天类的长连接 -->  
        <!-- 使用Jetty,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-jetty</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-test</artifactId>  
            <scope>test</scope>  
        </dependency>  
</dependencies>  

SpringBoot把容器修改为undertow

Xml代码 收藏代码

<dependencies>  
        <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>  
          
        <!-- undertow不支持jsp -->  
        <!-- 使用undertow,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-undertow</artifactId>  
        </dependency>  
          
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-test</artifactId>  
            <scope>test</scope>  
        </dependency>  
</dependencies>  

为什么可以这样切换呢?

因为SpringBoot在org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration类中已经配置好,根据依赖的Jar包自动切换,代码如下:

Java代码 收藏代码

/** 
     * Nested configuration if Tomcat is being used. 
     */  
    @Configuration  
    @ConditionalOnClass({ Servlet.class, Tomcat.class })  
    @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)  
public static class EmbeddedTomcat {  

    @Bean  
    public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {  
        return new TomcatEmbeddedServletContainerFactory();  
    }  

}  

/** 
     * Nested configuration if Jetty is being used. 
     */  
@Configuration  
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class,  
                     WebAppContext.class })  
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)  
public static class EmbeddedJetty {  

    @Bean  
    public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {  
        return new JettyEmbeddedServletContainerFactory();  
    }  

}  

/** 
     * Nested configuration if Undertow is being used. 
     */  
@Configuration  
@ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })  
@ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)  
public static class EmbeddedUndertow {  

    @Bean  
    public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {  
        return new UndertowEmbeddedServletContainerFactory();  
    }  

}  

方法上注解根据依赖的容器Jar包判断使用哪个容器:

如:

1、tomcat容器

Java代码 收藏代码

@ConditionalOnClass({ Servlet.class, Tomcat.class })  

表示有使用类Tomcat.class则是tomcat容器

2、Jetty容器

Java代码 收藏代码

@ConditionalOnClass({ Servlet.class, Server.class, Loader.class,  
            WebAppContext.class })  

3、undertow容器

Java代码 收藏代码

@ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })  
posted @ 2021-07-27 13:30  satire  阅读(403)  评论(0)    收藏  举报