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下

  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容器的依赖包,如下:

  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

  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包自动切换,代码如下:

  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容器

  1. @ConditionalOnClass({ Servlet.class, Tomcat.class })

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

2、Jetty容器

  1. @ConditionalOnClass({ Servlet.class, Server.class, Loader.class,
  2. WebAppContext.class })

3、undertow容器

  1. @ConditionalOnClass({ Servlet.class, Undertow.class, SslClientAuthMode.class })

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

©Copyright 蕃薯耀 2018年3月29日

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

SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器的更多相关文章

  1. SpringBoot切换Tomcat容器

    SpringBoot默认的容器为Tomcat, 依赖包在spring-boot-starter-web下 Xml代码 <dependencies> <dependency> & ...

  2. springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布

    一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...

  3. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  4. 蛋疼的springboot web项目使用jetty容器运行

    出现的问题: 今天自己新建了一个maven webapp项目,准备自己看看springboot的东西,搭好的项目是这样的 一切都很正常啊,用run App的方式直接启动 成功啦,本应该到此结束,喝茶吃 ...

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

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

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

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

  7. SpringBoot2使用Jetty容器(替换默认Tomcat)

    https://blog.csdn.net/hanchao5272/article/details/99649252   Jetty和tomcat的比较 Tomcat和Jetty都是一种Servlet ...

  8. SpringBoot启动tomcat源码解读

    一.SpringBoot自动拉起Tomcat 原文链接:http://www.studyshare.cn/blog-front/blog/details/1136 SpringBoot框架是当前比较流 ...

  9. SpringBoot系列六:SpringBoot整合Tomcat

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Tomcat 2.背景 SpringBoot 本身支持有两类的 WEB 容器:默认的 To ...

随机推荐

  1. ie6定位absolute bug触发layout解决

    IE6中很多Bug都可以通过触发layout得到解决,以上的解决方法无论是设置zoom:1还是设置width和height其实都是为了触发layout.下列的CSS属性或取值会让一个元素获得layou ...

  2. Go 语言环境搭建

    本文内容 概述 Go SDK LiteIDE 参考资料 2009年Google推出了它的第二个开源语言 Go.对 Go 的评价褒贬不一,中国比国外的热情高.Go 天生就是为并发和网络而生的,除了这点外 ...

  3. Java 8 学习资料汇总【转载】

    原文地址 2014年3月18日,Java SE 8 发布,而 Java 9 预期2016年发布: 2011年7月7日,Java 7 发布,是2009年4月20日被Oracle 以74亿美元收购首次推出 ...

  4. webservice 配置

    webservice 配置 <system.web> <!--允许GET/POST请求 --> <webServices> <protocols> &l ...

  5. 微软BI 之SSIS 系列 - 对于平面文件中 NULL 值处理过程中容易极易混淆的几个细节

    开篇介绍 最近有人问我有关文件处理中空值处理的相关问题: OLE DB Destination 中的 Keep Nulls 如何控制 NULL 值的显示? 为什么选中了 Keep Nulls 但是数据 ...

  6. Java并发编程系列之三十二:丢失的信号

    这里的丢失的信号是指线程必须等待一个已经为真的条件,在開始等待之前没有检查等待条件.这样的场景事实上挺好理解,假设一边烧水,一边看电视,那么在水烧开的时候.由于太投入而没有注意到水被烧开. 丢失的信号 ...

  7. linux popen()与system()的区别

    linux popen()与system()的区别 popen() 可以在调用程序和POSIX shell /usr/bin/sh 要执行的命令之间创建一个管道(请参阅sh-posix(1) ). p ...

  8. 【概念原理】四种SQL事务隔离级别和事务ACID特性

    摘要: SQL事务隔离级别和事务的ACID特性 事务是一组读写操作,并且具有只有所有操作都成功才算成功的特性.   事务隔离级别 SQL事务隔离级别由弱到强分别是:READ_UNCOMMITTED.R ...

  9. 如何查看memcache的性能

    memcache的运行状态可以方便的用 stats 命令显示.首先用telnet 127.0.0.1 11211这样的命令连接上memcache,然后直接输入stats就可以得到当前memcache的 ...

  10. RabbitMQ使用技巧

    一. net客户端介绍    http://www.cnblogs.com/hsyzero/p/6297644.html 二. RabbitMQ消息应答 执行一个任务可能需要花费几秒钟,你可能会担心如 ...