【启动报错】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean报错明细报错原因解决办法后记报错明细远程仓库pull下来的Spring boot项目,依赖导入完成后,IDEA里运行启动类,报错:Error starting ApplicationContext. To dis
·
【启动报错】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
报错明细
远程仓库pull下来的Spring boot项目,依赖导入完成后,IDEA里运行启动类,报错:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-09 17:58:12.660 ERROR --- [ main] ot.SpringApplication : Application run failed
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:161)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.topcheer.adp.AdpApplication.main(AdpApplication.java:29)
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:205)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:177)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158)
... 9 common frames omitted
报错原因
报错信息说的很清楚,找名叫ServletWebServerFactory的bean,由于是生产库pull直接下来的,代码本身应该不会有什么问题,问题是出在了我本地环境中。
在我的pom里面有这样一项依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
其中scope属性值为provided,对于scope属性的可选值释义如下:
- compile (编译):为默认范围,其意义在于默认没有提供一个范围,则该项目依赖的范围就是编译范围。使用该值会导致编译范围依赖在所有的classpath中是可用的,同时在打包时候将包含这些依赖;
- provided (已提供):依赖只有在当JDK 或者一个容器已提供该依赖之后才使用。例如, 如果你开发了一个web 应用,你可能在编译 classpath 中需要可用的Servlet API 来编译一个servlet,但是你不会想要在打包好的WAR 中包含这个Servlet API;这个Servlet API JAR 由你的应用服务器或者servlet 容器提供。已提供范围的依赖在编译classpath (不是运行时)可用。它们不是传递性的,也不会被打包;
- runtime (运行时):依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC
驱动实现; - test (测试):范围依赖 在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用;
- system (系统):范围依赖与provided类似,但是你必须显式的提供一个对于本地系统中JAR文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构建应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个systemPath元素。注意该范围是不推荐使用的(建议尽量去从公共或定制的 Maven 仓库中引用依赖);
根据各属性值的范围描述,可推断为我的依赖范围可能有问题(这点也足可见ECLIPSE和IDEA,以及IDEA新老版本启动项目之间的差异),那由于该项目是不希望在打包的时候将标注为provided的包注入依赖项中,因此我们不能通过修改scope的值来解决这一报错(实际上将值修改为compile是能够避免该错误的)。
解决办法
打开IDEA Edit Configurations页面:
勾选Include dependenceies with “Provided” scope选项,然后点击OK保存:
之后再运行项目就不会有该报错,问题解决。
后记
大家有什么问题可下方留言交流。
更多推荐
已为社区贡献1条内容
所有评论(0)