You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot启动异常:含test依赖无法启动Tomcat/启动后无报错终止

解决Spring Boot的两个启动问题

嘿,我来帮你搞定这两个Spring Boot的常见问题,都是我平时帮开发者排查过很多次的情况:

问题1:包含spring-boot-starter-test依赖时无法启动Tomcat

这个问题几乎都是依赖范围配置错误导致的:spring-boot-starter-test是专门给测试环境用的依赖包,如果你把它放到了compile或者默认的主依赖范围(而不是test范围),Spring Boot会自动加载测试相关的自动配置,这些配置会干扰Web服务器的启动逻辑。

解决方案:

  • 检查你的构建文件,把spring-boot-starter-test的范围设置为test:
    Maven(pom.xml)示例:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <!-- 较新版本Spring Boot可添加此排除,避免JUnit版本冲突 -->
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    Gradle(build.gradle)示例:
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
  • 如果你确实需要在主代码中用到测试依赖里的某个库,别直接引入整个starter-test,单独引入你需要的那个依赖就行,避免引入不必要的测试配置。

问题2:Spring Boot Web应用启动后立即停止,无错误日志

从你提供的日志来看,Tomcat刚启动就没下文了,这说明Spring Boot没识别出这是一个Web应用,启动完上下文就直接关闭了。常见原因有这几个:

可能的原因&解决方案:

  1. 缺少Web核心依赖
    你的项目里大概率没加spring-boot-starter-web,这是Spring Boot Web应用的核心依赖,没有它的话,Spring Boot不会启动Tomcat这类Web服务器。
    赶紧加上这个依赖:
    Maven示例:

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

    Gradle示例:

    implementation 'org.springframework.boot:spring-boot-starter-web'
    
  2. 主启动类注解配置错误
    检查你的主启动类,确保它标注了@SpringBootApplication注解(这个注解包含了@Configuration、@EnableAutoConfiguration、@ComponentScan三个核心注解,能自动启用Web相关配置):

    @SpringBootApplication
    public class YourWebApplication {
        public static void main(String[] args) {
            SpringApplication.run(YourWebApplication.class, args);
        }
    }
    
  3. Web应用类型配置错误
    检查你的配置文件(application.properties/application.yml),有没有不小心设置了:

    • properties文件:spring.main.web-application-type=none
    • yml文件:spring.main.web-application-type: none
      这个配置会让Spring Boot以非Web应用模式启动,自然不会保持Tomcat运行。如果需要Web应用,把这个配置删掉就行(默认就是servlet类型)。

内容的提问来源于stack exchange,提问作者Neo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:00:29