在pom.xml文件中,排除SpringBoot自带的嵌入tomcat,添加外置的tomcat依赖
时间: 2024-10-21 10:04:55 浏览: 132
在Spring Boot项目中,如果你想要使用外部Tomcat服务器而不是内置的Jetty或Tomcat,你需要在`pom.xml`文件中做以下几个步骤:
1. **移除默认的嵌入式容器依赖**:首先,删除`spring-boot-starter-web`或包含内建Web容器的类似依赖,因为这通常会自动引入Spring Boot的内建支持。例如:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional> <!-- 设置为可选,防止它隐式存在 -->
</dependency>
```
2. **添加Tomcat服务器的依赖**:然后,直接添加Tomcat服务器的starter包,如`spring-boot-starter-tomcat`:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> <!-- 指定在运行时由容器提供 -->
</dependency>
```
3. **配置启动类**:如果需要,可以在`@SpringBootApplication`注解上添加`run défininder = "org.springframework.boot.devtools.restart.RestartLauncher"`来禁用内建的热部署工具。
4. **设置应用上下文路径**:如果你想让Spring Boot应用在特定的URL路径下运行,可以配置`server.servlet.context-path`属性,比如:
```xml
<properties>
<server.servlet.context-path>/your-context-path</server.servlet.context-path>
</properties>
```
注意:由于`spring-boot-starter-tomcat`依赖提供的已经是完整的Tomcat环境,所以不需要额外安装独立的Tomcat服务器。
阅读全文
相关推荐










