IDEA启动springboot项目报missing ServletWebServerFactory错误的解决方案

 更新时间:2025年12月25日 15:49:45   作者:百***5588  
Spring Boot项目在IDEA中启动时报错,经过排查发现是由于Tomcat依赖的AprLifecycleListener类未被正确加载,通过将Tomcat依赖的scope从provided改为compile,解决了问题

该问题出现原因多样,大多数是因为配置不当的问题,首先要确定自己问题是不是与本问题出现原因一样。

背景

项目在IDEA突然就报错了

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-18 16:52:29.850 ERROR 9248 --- [           main] o.s.boot.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:157) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at com.ynkg.pongal.PongalApplication.main(PongalApplication.java:22) [classes/:na]
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:206) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    ... 8 common frames omitted​

一大串错误的核心信息是

org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

网上查找内容大多说的是不是配置没配好之类的,然而都没法解决我的情况,所以只好自己研究了。

过程

一阵断定定位,最终到了

//ServletWebServerApplicationContext
protected ServletWebServerFactory getWebServerFactory() {
		// Use bean names so that we don't consider the hierarchy
		String[] beanNames = getBeanFactory()
				.getBeanNamesForType(ServletWebServerFactory.class);
		if (beanNames.length == 0) {
			throw new ApplicationContextException(
					"Unable to start ServletWebServerApplicationContext due to missing "
							+ "ServletWebServerFactory bean.");
		}
		if (beanNames.length > 1) {
			throw new ApplicationContextException(
					"Unable to start ServletWebServerApplicationContext due to multiple "
							+ "ServletWebServerFactory beans : "
							+ StringUtils.arrayToCommaDelimitedString(beanNames));
		}
		return getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
	}

貌似没啥特殊的,

大概就是说加载不到ServletWebServerFactory,IDEA定位下,可以发现,这个工厂接口有3个实现类

  • JettyServletWebServerFactory
  • TomcatServletWebServerFactory
  • UndertowServletWebServerFactory

回到问题,无法实例化Bean出现的错误,那么是不是可以考虑手动标记呢?

处理

给启动类添加上Bean声明,也就是当启动时,web服务容器会加载这么一个bean

	@Bean
	ServletWebServerFactory servletWebServerFactory(){
		return new TomcatServletWebServerFactory();
	}

运行项目,发现依然报错了,万幸的是,这次错误信息有变化了,很长一段。

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-18 17:10:46.352 ERROR 11224 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletWebServerFactory' defined in com.ynkg.pongal.PongalApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.server.ServletWebServerFactory]: Factory method 'servletWebServerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/catalina/core/AprLifecycleListener
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at com.ynkg.pongal.PongalApplication.main(PongalApplication.java:22) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletWebServerFactory' defined in com.ynkg.pongal.PongalApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.server.ServletWebServerFactory]: Factory method 'servletWebServerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/catalina/core/AprLifecycleListener
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:216) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    ... 8 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.server.ServletWebServerFactory]: Factory method 'servletWebServerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/catalina/core/AprLifecycleListener
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/catalina/core/AprLifecycleListener
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getDefaultLifecycleListeners(TomcatServletWebServerFactory.java:158) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.<init>(TomcatServletWebServerFactory.java:114) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at com.ynkg.pongal.PongalApplication.servletWebServerFactory(PongalApplication.java:33) [classes/:na]
    at com.ynkg.pongal.PongalApplication$$EnhancerBySpringCGLIB$$951efaf5.CGLIB$servletWebServerFactory$1(<generated>) ~[classes/:na]
    at com.ynkg.pongal.PongalApplication$$EnhancerBySpringCGLIB$$951efaf5$$FastClassBySpringCGLIB$$d7e2ff01.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at com.ynkg.pongal.PongalApplication$$EnhancerBySpringCGLIB$$951efaf5.servletWebServerFactory(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.catalina.core.AprLifecycleListener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_191]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_191]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_191]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_191]
    ... 34 common frames omitted

核心错误信息是

Caused by: java.lang.ClassNotFoundException: org.apache.catalina.core.AprLifecycleListener.

继续定位该类AprLifecycleListener,发现是tomcat-embed-core-9.0.19.jar包下的东西,那为何会找不到呢?

<!--pom.xml-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provide</scope>
</dependency>

这里的scope配置的是provide,也就是说,在编译打包时,lib里不会存在这个artifact,以防止与容器冲突。

然而在idea的时候,却因此读取不到这个class导致了问题(具体原因目前已触及我知识盲区)。

总之,把scope的值改为compile(默认值)即可,到发布正式环境时记得改回去就可以了(不需要配置TomcatServletWebServerFactory bean)。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java二叉树的遍历思想及核心代码实现

    Java二叉树的遍历思想及核心代码实现

    今天小编就为大家分享一篇关于Java二叉树的遍历思想及核心代码实现,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • spring boot executable jar/war 原理解析

    spring boot executable jar/war 原理解析

    spring boot里其实不仅可以直接以 java -jar demo.jar的方式启动,还可以把jar/war变为一个可以执行的脚本来启动,比如./demo.jar,这篇文章主要介绍了spring boot executable jar/war 原理,需要的朋友可以参考下
    2023-02-02
  • Java并发编程中高频异常的原因,场景与解决方案全解析

    Java并发编程中高频异常的原因,场景与解决方案全解析

    在Java并发编程中,异常处理是保障程序稳定性的核心环节,本文将深度解析4类高频并发异常的产生原因、典型场景,并给出可落地的解决方案,帮你彻底避开这些坑
    2026-03-03
  • SpringBoot定时任务@Scheduled使用方式

    SpringBoot定时任务@Scheduled使用方式

    本文主要介绍了SpringBoot中的定时任务的实现方法,通过@EnableScheduling开启定时任务,使用@Scheduled注解可以设置定时任务的触发方式,文中详细介绍了corn参数的使用方法和各种通配符的含义,并给出了示例代码
    2026-04-04
  • springboot2中设置@ApiImplicitParam的dataType不起作用的解决

    springboot2中设置@ApiImplicitParam的dataType不起作用的解决

    本文主要介绍了在SpringBoot2中使用Swagger时,@ApiImplicitParam的dataType属性不起作用的问题及其解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-03-03
  • SpringCloud对服务内某个client进行单独配置的操作步骤

    SpringCloud对服务内某个client进行单独配置的操作步骤

    我们的微服务项目用的是springCloud,某个微服务接口因为数据处理量大,出现了接口超时的情况,我们需要单独修改这一个feignClient的超时时间,所以本文介绍了SpringCloud对服务内某个client进行单独配置的操作步骤,需要的朋友可以参考下
    2023-10-10
  • MyBatis输入映射和输出映射实例详解

    MyBatis输入映射和输出映射实例详解

    mapper.xml是我们配置操作数据库的sql语句的地方.这篇文章主要介绍了MyBatis输入映射和输出映射实例详解,需要的朋友可以参考下
    2017-02-02
  • SpringBoot中整合knife4j接口文档的实践

    SpringBoot中整合knife4j接口文档的实践

    这篇文章主要介绍了SpringBoot中整合knife4j接口文档的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • SpringBoot整合dataworks的实现过程

    SpringBoot整合dataworks的实现过程

    这篇文章主要介绍了SpringBoot整合dataworks的实现过程,实现主要是编写工具类,如果需要则可以配置成SpringBean,注入容器即可使用,需要的朋友可以参考下
    2022-08-08
  • SpringBoot读取resource目录下文件失败的原因及解决方案

    SpringBoot读取resource目录下文件失败的原因及解决方案

    在idea中运行时,有些resource下文件读取工具类能够正常获取读取到文件,但是通过java–jar的方式去运行jar包,此时resource下文件读取工具类读取文件就失效了,本文就给大家介绍一下SpringBoot读取resource目录下文件失败解决方案,需要的朋友可以参考下
    2023-08-08

最新评论