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

SpringBoot 2.5.0 MVC项目部署Tomcat后无启动日志问题排查

问题原因及修复方案

1. pom.xml配置错误与缺失

  • 未指定打包格式为war,需在project节点下添加配置:<packaging>war</packaging>
  • 未排除内嵌Tomcat容器,会和外部部署的Tomcat产生冲突,需将内置Tomcat的依赖 scope 设为provided
  • 多余的spring-webmvc依赖:spring-boot-starter-web已经内置了匹配版本的spring-webmvc,单独引入可能引发版本冲突,建议移除

修正后的pom核心配置如下:

<packaging>war</packaging>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.0</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- 排除内置Tomcat -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- 引入Tomcat依赖,scope设为provided,编译使用,打包不会打入war包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

2. 主启动类配置错误

  • 继承SpringBootServletInitializer后,必须重写configure方法指定启动类,这是SpringBoot项目部署到外部Tomcat的必填配置,缺少该配置时Tomcat无法识别SpringBoot项目的启动入口,不会触发项目初始化逻辑,所以你的打印和日志都不会输出
  • 你重写了onStartup方法但没有调用父类的onStartup逻辑,父类的Spring上下文初始化逻辑不会执行,即使配置了configure也无法正常启动项目,如果需要自定义onStartup逻辑必须先调用父类方法

修正后的主启动类代码如下:

@SpringBootApplication
@EnableWebMvc
@ComponentScan("com.xxx")
public class Application extends SpringBootServletInitializer implements ApplicationListener<ApplicationReadyEvent> {

    static final Logger logger = LoggerFactory.getLogger(Application.class);
    ServletContext servletContext;
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args );
    }

    // 新增重写configure方法,指定启动类
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 必须先调用父类的onStartup方法执行初始化逻辑
        super.onStartup(servletContext);
        System.out.println("Testing STDOUT");
        logger.debug("Testing logger");
        WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
        this.servletContext = servletContext;
        if (rootAppContext != null) {
            servletContext.addListener(new RequestContextListener());
        }
        else {
            logger.debug("No ContextLoaderListener registered, as "
                    + "createRootApplicationContext() did not "
                    + "return an application context");
        }   
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent arg0) {
        System.out.println("onApplicationEvent Testing STDOUT");
    }
}

3. 日志级别适配

你的代码中使用logger.debug输出日志,SpringBoot默认日志级别为info,debug级别日志默认不会输出,如需查看可在application.properties配置文件中添加:

logging.level.com.xxx=debug

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.17 22:45:04