Spring Native构建的Spring Boot应用启动即退出,Tomcat未运行
核心错误为 Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context,导致该问题的关键原因有两点:
不必要的
SpringBootServletInitializer继承
你的应用类继承了SpringBootServletInitializer,这个类仅用于将Spring Boot应用打包为WAR包、部署到外部Servlet容器的场景。而Spring Native构建的是独立可执行文件,依赖内置Servlet容器的自动配置逻辑,继承该类会干扰Spring Boot的自动配置流程,导致负责创建内置Tomcat/Undertow/Jetty的ServletWebServerFactory无法被正确初始化。Dockerfile中ENTRYPOINT的写法错误
当前ENTRYPOINT使用数组模式时,将命令与参数合并为单个字符串,这会导致整个字符串被当作独立命令执行,而非将--spring.profiles.active=local作为参数传递给应用程序,可能进一步加剧配置加载异常。
1. 修改应用类,移除SpringBootServletInitializer继承
将应用类改为普通Spring Boot启动类,去掉不必要的继承:
@SpringBootApplication public class ChatbotMemberEventWorkerApplication { public static void main(String[] args) { SpringApplication.run(ChatbotMemberEventWorkerApplication.class, args); } }
2. 修复Dockerfile的ENTRYPOINT写法
使用数组模式时,需将命令与每个参数分开声明:
ENTRYPOINT ["./chatbot-member-event-worker", "--spring.profiles.active=local"]
或使用更直观的Shell模式:
ENTRYPOINT ./chatbot-member-event-worker --spring.profiles.active=local
3. 重新构建并启动
执行Docker构建命令重新生成镜像,启动后Spring Boot会自动初始化内置Servlet容器的ServletWebServerFactory Bean,Web服务即可正常启动并持续运行。
内容的提问来源于stack exchange,提问作者Alpcan Yıldız

