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. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11. </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. <!-- Jetty适合长连接应用,就是聊天类的长连接 -->
  13. <!-- 使用Jetty,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-jetty</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-test</artifactId>
  21. <scope>test</scope>
  22. </dependency>
  23. </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. <!-- undertow不支持jsp -->
  13. <!-- 使用undertow,需要在spring-boot-starter-web排除spring-boot-starter-tomcat,因为SpringBoot默认使用tomcat -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-undertow</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-test</artifactId>
  21. <scope>test</scope>
  22. </dependency>
  23. </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. @Bean
  9. public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
  10. return new TomcatEmbeddedServletContainerFactory();
  11. }
  12. }
  13. /**
  14. * Nested configuration if Jetty is being used.
  15. */
  16. @Configuration
  17. @ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
  18. WebAppContext.class })
  19. @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
  20. public static class EmbeddedJetty {
  21. @Bean
  22. public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
  23. return new JettyEmbeddedServletContainerFactory();
  24. }
  25. }
  26. /**
  27. * Nested configuration if Undertow is being used.
  28. */
  29. @Configuration
  30. @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })
  31. @ConditionalOnMissingBean(value = EmbeddedServletContainerFactory.class, search = SearchStrategy.CURRENT)
  32. public static class EmbeddedUndertow {
  33. @Bean
  34. public UndertowEmbeddedServletContainerFactory undertowEmbeddedServletContainerFactory() {
  35. return new UndertowEmbeddedServletContainerFactory();
  36. }
  37. }

方法上注解根据依赖的容器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 })

SpringBoot切换Tomcat容器的更多相关文章

  1. SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器

    SpringBoot切换Tomcat容器, SpringBoot修改为Jetty容器, SpringBoot使用undertow容器, SpringBoot使用Jetty容器 ============ ...

  2. Springboot关于tomcat容器配置、三大组件配置、拦截器配置

    原文地址:http://www.javayihao.top/detail/172 1.tomcat配置 Springboot默认使用的就是嵌入式servlet容器即tomcat,对于web项目,如果使 ...

  3. 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中

    前言:本项目基于maven构建 spring-boot项目可以快速构建web应用,其内置的tomcat容器也十分方便我们的测试运行: spring-boot项目需要部署在外部容器中的时候,spring ...

  4. SpringBoot项目部署在同一个tomcat容器报错

    在一个Tomcat容器中部署了两个springboot的应用,在启动时发现一直都是第一个启动的项目能启动成功,第二个项目启动报错,错误信息如下: 2018-01-30 15:49:27.810 ERR ...

  5. SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)

    上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...

  6. SpringBoot 项目如何在tomcat容器中运行

    一. SpringBoot内嵌容器的部署方式 SpringBoot内部默认提供内嵌的tomcat容器,所以可以直接打成jar包,丢到服务器上的任何一个目录,然后在当前目录下执行java -jar de ...

  7. Springboot以Tomcat为容器实现http重定向到https的两种方式

    1 简介 本文将介绍在Springboot中如何通过代码实现Http到Https的重定向,本文仅讲解Tomcat作为容器的情况,其它容器将在以后一一道来. 建议阅读之前的相关文章: (1) Sprin ...

  8. SpringBoot从Eclipse添加的Tomcat容器中启动

    SpringBoot的Web项目,想要在Eclipse中的Tomcat容器中启动运行需要做下面这两处改动 pom.xml <packaging>war</packaging> ...

  9. springboot项目打包部署在指定的tomcat容器中

    1.首先需要修改项目的打包方式,将package改为war <packaging>war</packaging> 2.移除spring boot web中的嵌入式tomcat ...

随机推荐

  1. JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?

    1.innerHTML 属性 (参考自<JavaScript高级程序设计>294页) 在读模式下,innerHTML 属性返回与调用元素的所有子节点(包括元素.注释和文本节点)对应的 HT ...

  2. 时间处理,类似"xxxx-xx-xxTxx:xx:xx187+0000"格式

    后端返回的时间:"2020-04-24T09:12:51.187+0000" 目标显示时间:2020-04-24   09:12:51 <!DOCTYPE html> ...

  3. jdbc pool java连接池技术

    1 ConnectPool .java: 2 3 package pool; 4 5 /** 6 * Title: ConnectPool.Java 7 * Description: 连接池治理器 8 ...

  4. 【JavaSE】异常

    Java异常 2019-07-06  22:16:29  by冲冲 1. 引例 任何程序都有出错的可能.比如代码少一个分号,那么运行的结果是 java.lang.Error.比如运行 System.o ...

  5. Python+selenium之键盘和鼠标事件

  6. 联盛德 HLK-W806 (五): W801开发板上手报告

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  7. [NOIP2017 提高组] 逛公园

    考虑先做一个\(dp\),考虑正反建图,然后按0边拓扑,然后按1到这里的最小距离排序,然后扩展这个\(f_{i,j}\),即多了\(j\)的代价的方案数.

  8. Codeforces 1503E - 2-Coloring(组合数学)

    Codeforces 题目传送门 & 洛谷题目传送门 考虑什么样的 2-染色方式是符合题目要求的,首先蓝.黄颜色所形成的连通块个数必须 \(\le 2\),否则一定不合法,而显然如果两种颜色连 ...

  9. DTOJ 4027:挖煤

    挖煤 [问题描述]众所周知, 小C是挖煤好手.今天他带着他的魔法镐子去挖煤 ,他的镐子一开始有$w$点魔力.他的挖煤 路线 上会依次 经过$n$个地点, 地点, 每个 地点是煤矿或者补给站,设小C当前 ...

  10. SNPEFF snp注释 (添加自己基因组)

    之间介绍过annovar进行对snp注释,今天介绍snpEFF SnpEff is a variant annotation and effect prediction tool. It annota ...