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

Spring Boot集成测试启动后异常终止,关联OpenAPI配置问题

问题分析与修复方案

核心问题总结

应用启动完成后立即触发终止流程,导致Maven集成测试阶段的spring-boot:start目标失败,无法连接到MBean服务器确认应用状态。

关键日志解读

从报错日志中可提取两个核心异常点:

  1. 应用启动后抛出的终止异常:
Exception in thread "main" java.lang.IllegalStateException: Shutdown in progress
    at java.base/java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66)
    at java.base/java.lang.Runtime.addShutdownHook(Runtime.java:216)
    at org.springframework.context.support.AbstractApplicationContext.registerShutdownHook(AbstractApplicationContext.java:995)
    at com.abcd.ocs.someservice.Application.start(Application.java:63)
    at com.abcd.ocs.someservice.Application.main(Application.java:46)

说明Spring上下文尝试注册关闭钩子时,JVM已处于终止流程中,应用启动后立即被触发关闭。

  1. Maven插件最终报错:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.5.7:start (pre-integration-test) on project some-service: Could not figure out if the application has started: Failed to connect to MBean server at port 9001: Could not invoke shutdown operation: Spring application did not start before the configured timeout (30000ms -> [Help 1]

插件无法连接到MBean服务器,无法确认应用启动状态,最终超时失败。

pom.xml配置问题排查

提供的插件配置存在两处明显错误:

1. JVM参数重复配置被覆盖

原配置中jvmArguments被定义两次:

<jvmArguments>-Dspring.application.admin.enabled=true</jvmArguments>
<jvmArguments>-Dspring-boot.run.profiles=ci</jvmArguments>

Maven插件的jvmArguments会以最后一次定义为准,导致-Dspring.application.admin.enabled=true被覆盖,MBean服务器未启用,插件无法通过MBean检测应用状态。

2. Profiles配置标签嵌套错误

原配置中profiles标签嵌套了无效的内层profiles:

<profiles>
    <profiles>${app.profiles}</profiles>
</profiles>

正确配置应直接指定激活的profile,无需嵌套,否则配置不生效。

修复步骤

1. 合并JVM参数,确保MBean服务器启用

将两个jvmArguments合并为一行,保留所有必要参数:

<jvmArguments>-Dspring.application.admin.enabled=true -Dspring-boot.run.profiles=ci</jvmArguments>

2. 修正Profiles配置格式

将嵌套的profiles标签改为正确格式,直接指定激活的profile:

<profiles>${app.profiles}</profiles>

若需指定多个profile,用逗号分隔,例如<profiles>ci,test</profiles>。

3. 检查自定义Application类的启动逻辑

异常来自com.abcd.ocs.someservice.Application.start方法,需确认:

  • 是否在启动完成后调用了System.exit()或类似终止JVM的代码
  • Spring上下文初始化逻辑是否正常,无意外触发关闭事件的情况

4. 调整插件超时时间(可选)

日志显示应用启动耗时43秒,当前30秒超时不足以检测启动完成,可在插件配置中增加超时参数:

<configuration>
    <!-- 其他配置 -->
    <wait>60000</wait> <!-- 设置为60秒超时 -->
</configuration>

验证修复

修改配置后重新执行集成测试,确认:

  1. 应用启动后不再立即终止
  2. Maven插件能成功连接到MBean服务器,确认应用启动状态
  3. 集成测试流程正常完成

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:15:52