SpringBoot切換Tomcat容器,SpringBoot使用Jetty容器

SpringBoot切換Tomcat容器,web

SpringBoot修改成Jetty容器,spring

SpringBoot使用undertow容器,tomcat

SpringBoot使用Jetty容器jsp

================================spring-boot

©Copyright 蕃薯耀 2018年3月29日spa

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

 

附件&源碼下載見:http://fanshuyao.iteye.com/blog/2414809blog

 

1、SpringBoot默認的容器爲Tomcat,依賴包在spring-boot-starter-web下ci

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>  

 

 

2、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>  

 

3、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>  

 

4、爲何能夠這樣切換呢?

由於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包判斷使用哪一個容器:

如:

一、tomcat容器

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

表示有使用類Tomcat.class則是tomcat容器

 

二、Jetty容器

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

 

三、undertow容器

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

 

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

©Copyright 蕃薯耀 2018年3月29日

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

相關文章
相關標籤/搜索