之前还好好的,今天打开后就会出现这种错误,然后进行了各种百度,还是没有解决,又重新创建了一个springboot发现也可以实现,这就很迷了。

Run中的内容:

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:162) ~[spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.2.jar:5.3.2]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) [spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) [spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) [spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) [spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) [spring-boot-2.4.1.jar:2.4.1]
	at com.zt.BootWebAdminApplication.main(BootWebAdminApplication.java:10) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.4.1.jar:2.4.1]
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:209) ~[spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.4.1.jar:2.4.1]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:159) ~[spring-boot-2.4.1.jar:2.4.1]
	... 14 common frames omitted

对应的中文意思是:由于缺少ServletWebServerFactory bean,因此无法启动ServletWebServerApplicationContext。
打开项目配置:

打开配置后发现在springboot模板下的这个主配置类竟然爆红,找不到主配置类,那肯定是自动配置的问题啊,然后在一篇博主上看到,在主配置中加上@EnableAutoConfiguration果不其然加上后果然好了就是自动配置的问题,但是@SpringBootApplication不是已经包括@EnableAutoConfiguration这个注解了吗,为什么还要单独加上呢,心想是不是import错了包,对比了之前的项目,果然import错了,我导的是这个import org.springframework.boot.SpringBootConfiguration;。实际上@SpringBootConfiguration应该导import org.springframework.boot.autoconfigure.SpringBootApplication;这个包!!!我竟然写成了@SpringBootConfiguration.
其实@SpringBootApplication包含了@ComponentScan,@EnableAutoConfiguration,@SpringBootConfiguration.这三个注解!

低级错误引以为戒,一个注解的事,当然不适合所有此类错误的解决,但是一般就是自动配置的问题!!!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐