java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > 程序启动报错org.springframework.context.ApplicationContextException: Unable to start web server

解决程序启动报错org.springframework.context.ApplicationContextException: Unable to start web server问题

作者:天黑请​闭眼

文章描述了一个Spring Boot项目在不同环境下启动时出现差异的问题,通过分析报错信息,发现是由于导入`spring-boot-starter-tomcat`依赖时定义的scope导致的配置问题,调整依赖导入配置后,解决了启动错误

一、场景

1、该项目在Linux和本机电脑上启动都正常

2、在另一台电脑上启动就报错

3、代码都是同一份,没有差别

二、报错信息

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.xxx.RobotManagerSvr.main(RobotManagerSvr.java:32)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:206)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
    ... 8 common frames omitted

三、原因

导入spring-boot-starter-tomcat依赖时定义的scope导致

原依赖导入配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
    <!--是provided导致-->
	<scope>provided</scope>
</dependency>

四、解决

调整后依赖导入配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
    <!--测试时先注释该配置-->
	<!--<scope>provided</scope>-->
</dependency>

注:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文