• SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器


    SpringBoot切换Tomcat容器,

    SpringBoot修改为Jetty容器,

    SpringBoot使用undertow容器,

    SpringBoot使用Jetty容器

    ================================

    ©Copyright 蕃薯耀 2018年3月29日

    http://www.cnblogs.com/fanshuyao/

    附件&源码下载见:http://fanshuyao.iteye.com/blog/2414809

    一、SpringBoot默认的容器为Tomcat,依赖包在spring-boot-starter-web下

    Xml代码  收藏代码
    1. <dependencies>  
    2.         <dependency>  
    3.             <groupId>org.springframework.boot</groupId>  
    4.             <artifactId>spring-boot-starter-web</artifactId>  
    5.         </dependency>  
    6.   
    7.         <dependency>  
    8.             <groupId>org.springframework.boot</groupId>  
    9.             <artifactId>spring-boot-starter-test</artifactId>  
    10.             <scope>test</scope>  
    11.         </dependency>  
    12.   
    13. </dependencies>  

     

     

    二、SpringBoot把容器修改为Jetty

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

    Xml代码  收藏代码
    1. <dependencies>  
    2.         <dependency>  
    3.             <groupId>org.springframework.boot</groupId>  
    4.             <artifactId>spring-boot-starter-web</artifactId>  
    5.             <exclusions>  
    6.                 <exclusion>  
    7.                     <groupId>org.springframework.boot</groupId>  
    8.                     <artifactId>spring-boot-starter-tomcat</artifactId>  
    9.                 </exclusion>  
    10.             </exclusions>  
    11.         </dependency>  
    12.   
    13.         <!-- Jetty适合长连接应用,就是聊天类的长连接 -->  
    14.         <!-- 使用Jetty,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->  
    15.         <dependency>  
    16.             <groupId>org.springframework.boot</groupId>  
    17.             <artifactId>spring-boot-starter-jetty</artifactId>  
    18.         </dependency>  
    19.         <dependency>  
    20.             <groupId>org.springframework.boot</groupId>  
    21.             <artifactId>spring-boot-starter-test</artifactId>  
    22.             <scope>test</scope>  
    23.         </dependency>  
    24. </dependencies>  

    三、SpringBoot把容器修改为undertow

    Xml代码  收藏代码
    1. <dependencies>  
    2.         <dependency>  
    3.             <groupId>org.springframework.boot</groupId>  
    4.             <artifactId>spring-boot-starter-web</artifactId>  
    5.             <exclusions>  
    6.                 <exclusion>  
    7.                     <groupId>org.springframework.boot</groupId>  
    8.                     <artifactId>spring-boot-starter-tomcat</artifactId>  
    9.                 </exclusion>  
    10.             </exclusions>  
    11.         </dependency>  
    12.           
    13.         <!-- undertow不支持jsp -->  
    14.         <!-- 使用undertow,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->  
    15.         <dependency>  
    16.             <groupId>org.springframework.boot</groupId>  
    17.             <artifactId>spring-boot-starter-undertow</artifactId>  
    18.         </dependency>  
    19.           
    20.         <dependency>  
    21.             <groupId>org.springframework.boot</groupId>  
    22.             <artifactId>spring-boot-starter-test</artifactId>  
    23.             <scope>test</scope>  
    24.         </dependency>  
    25. </dependencies>  

    四、为什么可以这样切换呢?

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

    Java代码  收藏代码
    1. /** 
    2.      * Nested configuration if Tomcat is being used. 
    3.      */  
    4.     @Configuration  
    5.     @ConditionalOnClass({ Servlet.class, Tomcat.class })  
    6.     @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)  
    7.     public static class EmbeddedTomcat {  
    8.   
    9.         @Bean  
    10.         public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {  
    11.             return new TomcatEmbeddedServletContainerFactory();  
    12.         }  
    13.   
    14.     }  
    15.   
    16.     /** 
    17.      * Nested configuration if Jetty is being used. 
    18.      */  
    19.     @Configuration  
    20.     @ConditionalOnClass({ Servlet.class, Server.class, Loader.class,  
    21.             WebAppContext.class })  
    22.     @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)  
    23.     public static class EmbeddedJetty {  
    24.   
    25.         @Bean  
    26.         public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {  
    27.             return new JettyEmbeddedServletContainerFactory();  
    28.         }  
    29.   
    30.     }  
    31.   
    32.     /** 
    33.      * Nested configuration if Undertow is being used. 
    34.      */  
    35.     @Configuration  
    36.     @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })  
    37.     @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)  
    38.     public static class EmbeddedUndertow {  
    39.   
    40.         @Bean  
    41.         public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {  
    42.             return new UndertowEmbeddedServletContainerFactory();  
    43.         }  
    44.   
    45.     }  

      

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

    如:

    1、tomcat容器

    Java代码  收藏代码
    1. @ConditionalOnClass({ Servlet.class, Tomcat.class })  

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

    2、Jetty容器

    Java代码  收藏代码
    1. @ConditionalOnClass({ Servlet.class, Server.class, Loader.class,  
    2.             WebAppContext.class })  

    3、undertow容器

    Java代码  收藏代码
    1. @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })  

    (如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!) 

    ================================

    ©Copyright 蕃薯耀 2018年3月29日

    http://www.cnblogs.com/fanshuyao/

  • 相关阅读:
    BZOJ3518 : 点组计数
    BZOJ2217 : [Poi2011]Lollipop
    李洪强经典面试题40-可能碰到的iOS笔试面试题-C语言
    对AFN的二次封装
    李洪强经典面试题39-iOS 程序员 6 级考试(答案和解释)
    iOS五种本地缓存数据方式
    iOS开发网络篇—数据缓存
    iOS中的通知
    李洪强漫谈iOS开发[C语言-048]-打印平方表
    李洪强漫谈iOS开发[C语言-047]-数列求和
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/8668059.html
Copyright © 2020-2023  润新知