首页   >   代码编程   >   JAVA开发

Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

由于springboot项目内嵌了tomcat,所以一般都会将项目打包成jar包来进行发布,而并非以前的war包,但是公司有一个项目依旧将springboot项目打成war包,然后丢到tomcat容器中运行,这样就需要兼容本地启动调试和线上运行,涉及到tomcat依赖的地方就需要格外注意,一不小心就会导致有一边儿会启动失败。

整理之前的笔记,发现以前还真碰到过这个问题,本地能正常启动,但是发布到线上的时候就报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean,这就是典型的tomcat依赖没处理好导致的问题

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '
_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)
2020-12-18 09:14:06,263 default [background-preinit] INFO o.h.v.i.u.Version 898 - HV000001: Hibernate Validator 6.1.5.Final
2020-12-18 09:14:06,301 default [main] INFO c.g.l.Application 936 - Starting Application on LAPTOP-9J4HVAON with PID 2568 (D:\workspace_idea\logistics\target\classes started by Administrator in D:\workspace_idea\logistics)
2020-12-18 09:14:06,301 default [main] INFO c.g.l.Application 936 - The following profiles are active: dev
2020-12-18 09:14:07,692 default [main] INFO o.s.d.r.c.RepositoryConfigurationDelegate 2327 - Multiple Spring Data modules found, entering strict repository configuration mode!
2020-12-18 09:14:07,695 default [main] INFO o.s.d.r.c.RepositoryConfigurationDelegate 2330 - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2020-12-18 09:14:07,760 default [main] INFO o.s.d.r.c.RepositoryConfigurationDelegate 2395 - Finished Spring Data repository scanning in 43ms. Found 0 Redis repository interfaces.
2020-12-18 09:14:08,198 default [main] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker 2833 - Bean '
redisConfig' of type [com.gude.logistics.config.RedisConfig$$EnhancerBySpringCGLIB$$dfa45a1c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-12-18 09:14:08,221 default [main] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker 2856 - Bean '
org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@2ffb3aec' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-12-18 09:14:08,226 default [main] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker 2861 - Bean '
methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-12-18 09:14:08,347 default [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext 2982 - Exception encountered during context initialization - cancelling refresh attempt: 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.
2020-12-18 09:14:08,357 default [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener 2992 -
Error starting ApplicationContext. To display the conditions report re-run your application with '
debug' enabled.
2020-12-18 09:14:08,368 default [main] ERROR o.s.b.SpringApplication 3003 - 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.gude.logistics.Application.main(Application.java:12)
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

因为线上已经使用了tomcat外部容器了,所以在打war包的时候就不在需要tomcat相关的jar包了,否则就会冲突。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--注释掉-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

搞定,收工!

雨落无影

QQ群Ⅰ: 686430774

QQ群Ⅱ: 718410762

QQ群Ⅲ: 638620451

QQ群Ⅳ: 474195684

QQ群Ⅴ: 463034360

QQ群Ⅵ: 879266502

QQ群Ⅶ: 627786015

工作5分钟,吹逼2小时: 855525339 (娱乐消遣,广告狗勿进)

如果文章有帮到你,可以考虑请博主喝杯咖啡!

分享到:

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2021-12-04

永久地址:https://www.jiweichengzhu.com/article/ca2d824340e446ff9c1e53d19d18058b

评论

雨落无影

关注上方公众号,回复关键字【下载】获取下载码

用完即删,每次下载需重新获取下载码

若出现下载不了的情况,请及时联系站长进行解决