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下
- <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/
SpringBoot切换Tomcat容器,SpringBoot使用Jetty容器的更多相关文章
- SpringBoot切换Tomcat容器
SpringBoot默认的容器为Tomcat, 依赖包在spring-boot-starter-web下 Xml代码 <dependencies> <dependency> & ...
- springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布
一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...
- SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...
- 蛋疼的springboot web项目使用jetty容器运行
出现的问题: 今天自己新建了一个maven webapp项目,准备自己看看springboot的东西,搭好的项目是这样的 一切都很正常啊,用run App的方式直接启动 成功啦,本应该到此结束,喝茶吃 ...
- Springboot关于tomcat容器配置、三大组件配置、拦截器配置
原文地址:http://www.javayihao.top/detail/172 1.tomcat配置 Springboot默认使用的就是嵌入式servlet容器即tomcat,对于web项目,如果使 ...
- Springboot以Tomcat为容器实现http重定向到https的两种方式
1 简介 本文将介绍在Springboot中如何通过代码实现Http到Https的重定向,本文仅讲解Tomcat作为容器的情况,其它容器将在以后一一道来. 建议阅读之前的相关文章: (1) Sprin ...
- SpringBoot2使用Jetty容器(替换默认Tomcat)
https://blog.csdn.net/hanchao5272/article/details/99649252 Jetty和tomcat的比较 Tomcat和Jetty都是一种Servlet ...
- SpringBoot启动tomcat源码解读
一.SpringBoot自动拉起Tomcat 原文链接:http://www.studyshare.cn/blog-front/blog/details/1136 SpringBoot框架是当前比较流 ...
- SpringBoot系列六:SpringBoot整合Tomcat
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Tomcat 2.背景 SpringBoot 本身支持有两类的 WEB 容器:默认的 To ...
随机推荐
- Android笔记(六):线程及线程通信
线程 由于Android的Activity中默认所有代码都在主线程(UI线程)中执行,如果在这里面执行耗时任务(例如下载),界面就会无反应且不可操作,直到耗时任务执行完毕. 如果想在执行耗时任务的同时 ...
- 使用uploadify上传图片时返回“Cannot read property 'queueData' of undefined”
在使用uploadify插件上传图片时,遇到一个比较坑的错误:上传时提示“Cannot read property 'queueData' of undefined”. 遇到这个问题有点无语,因为这个 ...
- 转:深入理解Java G1垃圾收集器
本文首先简单介绍了垃圾收集的常见方式,然后再分析了G1收集器的收集原理,相比其他垃圾收集器的优势,最后给出了一些调优实践. 一,什么是垃圾回收 首先,在了解G1之前,我们需要清楚的知道,垃圾回收是什么 ...
- SQLSERVER 设置默认值
DECLARE @test intSET @test=nullselect isnull(@test,0)
- 最新整合maven+SSM+Tomcat 实现注册登录
mybatis学习 http://www.mybatis.org/mybatis-3/zh/index.html Spring学习:http://blog.csdn.net/king1425/arti ...
- 在Asp.Net中操作PDF – iTextSharp - 操作图片
iTextSharp支持所有主流的图片格式,比如:jpg, tif, gif, bmp, png和wmf.在iTextSharp中使用Image.GetInstance()方法创建图片有很多种方式,或 ...
- react.js map遍历的问题
React遍历多个Ant Design中的Upload组件时,随意删除任一个Upload出现了bug,依次点击上传图片后,当点击删除时,倒着删除没有问题,从中间和从开头删问题出现了,出现了类似塌方的效 ...
- git 创建标签
在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branch 'ma ...
- 如何取消Visual Studio Browser Link
VS2013.2015新建MVC网站并浏览后,页面默认自动启用Browser Link功能 解决方法,只需要在web.config中添加配置节点即可 <appSettings> <a ...
- 【Python】python3.6中实现同一行动态输出
print同时需要设置flush=True来动态显示 print("\r[{0}] {1} <- {2}".format(username,time,jdTimeFomat) ...