Unable to start web server; nested exception is org.springframework.context.ApplicationContextExcept

SpringBoot项目启动报错,如图
在这里插入图片描述
Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.这个错误

可以看出ServletWebServerFactory在sping容器启动时没有将其注册进去,缺少相关依赖。没有对应的servelt容器,工程当然也就启动不起来。这里多讲一下,springboot工程会通过@EnableAutoConfiguration这个注解去拉去默认配置,该注解通过@Import注解导入了AutoConfigurationImportSelector类。其实这个类,就是导入通过加载配置文件,加载了很多工厂方法的配置类。最终会加载 META-INF/spring.factories这些文件,通过这些文件完成自动装配。@Import注解加载了tomcat,jetty,undertow三个web服务器的配置类。由于没有导入jetty和undertow的相关jar包,这两个类实例的不会真正的加载。到这里分析,为什么会缺少相应的servlet容器?springboot不默认加载了tomcat容器吗,因为tomcat的相关jar包已经在spring-boot-starter-web中引入了,按照之前我们的分析,实际上被加载的容器应该是tomcat,为什么还会找不到呢?

所以在项目里找spring-boot-starter-tomcat这个依赖,发现没有。。。。。
竟然没有!

手动添加

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<version>2.1.5.RELEASE</version>
	</dependency>

重新运行,可以了

当然还有各种其他问题也会导致没有注册到容器中,得自己一一排查

猜你喜欢

转载自blog.csdn.net/qq_42875600/article/details/130176394