spring boot更改默认tomcat容器

Published by xiaosixi on

Web容器是一种服务程序,在服务器一个端口就有一个提供相应服务的程序,而这个程序就是处理从客户端发出的请求,如JAVA中的Tomcat容器。SpringBoot官方推荐是富Jar模式运行,即使用内置容器(默认Tomcat)通过打包成Jar文件来运行。

常见的Web容器

  • Tomcat

  • Jetty

  • Undertom

  • 在SpringBoot中当引入spring-boot-starter-web默认支持的容器是tomcat.

  • SpringBoot2.x引入了一种新的编程风格:Webflux,当我们使用webflux即spring-boot-starter-webflux时候,默认支持的容器是Netty,当我们想使用最新的webflux的时候我们只用将spring-boot-starter-webflux替换掉spring-boot-starter-web即可使用Netty作为web容器来使用

Netty

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

但是当我们还是要使用普通的web方式,如何来指定默认的tomcat呢?其实也很简单,只用在maven中将spring-boot-starter-web包中的tomcat包移除,然后添加上要使用的web容器就可以了。pom文件可以这样写!

Tomcat

<dependency> 
	<groupId>org.springframework.boot</groupId> 
	<artifactId>spring-boot-starter-web</artifactId> 
</dependency>

Undertow

<dependency> 
	<groupId>org.springframework.boot</groupId> 
	<artifactId> spring-boot-starter-web</artifactId> 
	<exclusions> 
		<!- 排除Tomcat依赖项 - > 
		<exclusion> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-tomcat</artifactId> 
		</exclusion> 
	</exclusions> 
</dependency> 
<dependency> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter-undertow</artifactId> 
</dependency>

jetty

<properties> 
	<servlet-api.version> 3.1.0 < 
/servlet-api.version> </ properties> 
<dependency> 
	<groupId>org.springframework.boot</groupId> 
	<artifactId> spring-boot-starter-web</artifactId> 
	<exclusions> 
		<! - 排除Tomcat依赖项 - > 
		<exclusion> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-tomcat</artifactId> 
		</exclusion> 
	</exclusions> 
</dependency> 
<! - 使用Jetty - > 
<dependency> 
	<groupId>org.springframework.boot</groupId> 
	<artifactId>spring-boot-starter-jetty</artifactId> 
</dependency>

Categories: spring

xiaosixi

本人IT码农一枚,从事java企业级应用开发,目前就职于深圳某某公司,热衷于对技术的研究和热爱。本网站主要记录生活、工作上所发生所看的,可能大多来自互联网,如有版权问题,请联系,我会及时处理。

0 Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注