快速指南,在Spring Boot应用程序中排除嵌入式tomcat服务器并添加Jetty Server。 配置删除tomcat并添加Jetty Server。
1.简介
如何从Spring Boot应用程序中删除Tomcat服务器 。 实际上,一旦我们添加了“ spring-boot-starter-web
但是,Spring Boot使我们可以灵活地使用或不使用tomcat。 如果我们不希望我们可以排除此默认服务器。
默认情况下,Spring Boot带有3种类型的嵌入式服务器:Tomcat,Jetty和undertow。
排除tomcat和下一步添加jetty服务器
创建第一个Spring Boot应用程序以及如何测试Rest API。
2. Tomcat默认情况下
一旦我们将spring-boot-starter-web依赖关系作为pom.xml的一部分添加到使用spring boot进行Web应用程序开发中,它就会获得tomcat以及所有必需的依赖关系。 直接使用它并自动将其部署到tomcat总是很方便。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
但是,在某些情况下,当使用JMS而不是Web应用程序或想要添加Jetty时,不需要将tomcat用作Spring Boot应用程序的一部分。
3.排除Tomcat – Maven Pom.xml
要将tomcat从Spring Boot中排除,只需向Spring Boot Starter依赖项添加一个额外的块。 在依赖项部分,我们可以添加
<exclusions>标记,用于确保在构建时删除了给定的工件。
这是最简单的方法。
<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>
您可以使用该方法将Tomcat从Spring Boot中排除,也可以将其用于其他任何排除
4.排除Tomcat和所有服务器-注释
声明@SpringBootApplication批注时,有一种方法可以排除所有服务器,并且可以考虑像Web一样使用Spring Boot应用程序。
要将spring boot用作非Web应用程序,请使用以下命令。
@SpringBootApplication (exclude = {EmbeddedServletContainerAutoConfiguration. class , WebMvcAutoConfiguration. class })
并且需要将以下属性添加到非休息应用程序,以使Spring Boot不会尝试启动
WebApplicationContext
spring.main.web-environment= false
5.在Spring Boot中添加Jetty Server
如果您想在Spring启动应用程序中使用Jetty服务器,首先必须禁用默认的tomcat服务器,然后添加jetty依赖项“
spring-boot-starter-jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId> </dependency>
在pom.xml中添加了jetty之后,在构建时,它将禁用tomcat并映射到Jetty配置。
6. Gradle –排除tomcat并添加Jetty
那么,这很容易。 只需在排除部分添加tomcat,并在依赖项部分添加jetty。
configurations {
compile.exclude module: "spring-boot-starter-tomcat" } dependencies {
compile( "org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT" )
compile( "org.springframework.boot:spring-boot-starter-jetty:2.0.0.BUILD-SNAPSHOT" ) }
7.结论
在本文中,我们已经了解了如何通过pom.xml和注释级别禁用tomcat。 如果您注释级别,则它将完全禁用Web应用程序功能。 始终建议使用Maven排除。
并且还了解了如何添加Jetty服务器。