【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

简介: 【Java】Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

报错信息如下:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

在Spring Boot项目中,出现这个错误有几种情况:

一、启动类忘记添加注解

在main方法所在的类忘记添加@SpringBootApplication;

二、缺少依赖,添加即可

1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-web</artifactId>
4. </dependency>

三、启动类main方法写错

就是把SpringApplication.run(XxxApplication.class,args);写成了SpringApplication.run(SpringApplication.class,args);

应该是本项目的启动类名.class,如:SpringApplication.run(启动类名.class,args);

四、移除排除项

我试了第四个解决方案移除排除项是可以完美解决我的问题。

五、我的解决方案

在网上搜集了几种可能出现的问题原因,但是我的都不是这些问题,找了很久,终于找到了,是与pom.xml有关的,具体如下:

错误写法:

<!-- 这里指定打war包的时不再需要tomcat相关的包,但是本地运行时必须注释掉,否则会报错 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

正确写法:<scope>里的provided改为provide即可。

<!-- 这里指定打war包的时不再需要tomcat相关的包,但是本地运行时必须注释掉,否则会报错 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provide</scope>
        </dependency>

至于为什么还没有查清楚,后续再更新!

完结!


相关文章
|
1月前
|
容器
Unable to create tempDir. java.io.tmpdir is set to /tmp
Unable to create tempDir. java.io.tmpdir is set to /tmp
31 1
|
1天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
8 1
|
4天前
|
安全 Java
【JAVA】线程的run()和start()有什么区别?
【JAVA】线程的run()和start()有什么区别?
|
9天前
|
Java 测试技术 Spring
|
29天前
|
XML Java 程序员
作为Java程序员还不知道Spring中Bean创建过程和作用?
作为Java程序员还不知道Spring中Bean创建过程和作用?
15 0
|
2月前
|
XML 缓存 Java
Java常见bean 工具类性能对比
Java常见bean 工具类性能对比
37 0
|
2月前
|
Java 数据库 Spring
Spring Bean、Java Bean和对象的区别与联系
Spring Bean、Java Bean和对象的区别与联系
44 0
|
3月前
|
Java Linux iOS开发
8 种 Java- 内存溢出之五 -Unable to create new native thread
8 种 Java- 内存溢出之五 -Unable to create new native thread
|
4月前
|
Java Android开发 Windows
Java for Windows Missing问题原因分析以及完美解决方案
Java for Windows Missing问题原因分析以及完美解决方案
20 0
|
5月前
|
存储 Java
Java【报错 01】Unable to open nested jar file 问题说明及解决方法(仅针对压缩工具 WinRAR)
Java【报错 01】Unable to open nested jar file 问题说明及解决方法(仅针对压缩工具 WinRAR)
167 0