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下
- <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>
二、SpringBoot把容器修改為Jetty
方法很簡單,就是在pom.xml文件中,在引用的spring-boot-starter-web排除Tomcat的依賴包,然后再引入Jetty容器的依賴包,如下:
- <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
- <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包自動切換,代碼如下:
- /**
- * 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容器
- @ConditionalOnClass({ Servlet.class, Tomcat.class })
表示有使用類Tomcat.class則是tomcat容器
2、Jetty容器
- @ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
- WebAppContext.class })
3、undertow容器
- @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
(如果你覺得文章對你有幫助,歡迎捐贈,^_^,謝謝!)
================================
©Copyright 蕃薯耀 2018年3月29日
http://www.cnblogs.com/fanshuyao/