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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM