移除JPA数据源后Spring Boot Web应用启动失败:缺失ServletWebServerFactory Bean
问题
移除Spring Boot Web应用中所有JPA、Hibernate相关依赖、代码及application.yml对应配置项后,应用无法启动。pom.xml已引入spring-boot-starter-web依赖,仅在主类添加@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})注解时应用可正常启动,但这只是临时方案。
启动报错核心信息为:无法找到类型为org.springframework.boot.web.servlet.server.ServletWebServerFactory的合格Bean,导致Web应用无法启动。完整报错日志如下:
2025-04-09 15:09:17 [restartedMain] INFO c.t.m.d.p.myServer - Starting myServer using Java 11.0.2 on NB100005NQ0Q04 with PID 21364 2025-04-09 15:09:17 [restartedMain] INFO c.t.m.d.p.myServer - No active profile set, falling back to 1 default profile: "default" 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.boot.SpringApplication - Loading source class com.companygroup.mls.myapp.data2reader.myServer 2025-04-09 15:09:17 [restartedMain] INFO o.s.b.d.restart.ChangeableUrls - The Class-Path manifest attribute in C:\mavenRepo3\com\sun\xml\bind\jaxb-core\2.3.0\jaxb-core-2.3.0.jar referenced one or more files that do not exist: file:/C:/mavenRepo3/com/sun/xml/bind/jaxb-core/2.3.0/jaxb-api.jar 2025-04-09 15:09:17 [restartedMain] INFO o.s.b.d.restart.ChangeableUrls - The Class-Path manifest attribute in C:\mavenRepo3\com\sun\xml\bind\jaxb-impl\2.2.11\jaxb-impl-2.2.11.jar referenced one or more files that do not exist: file:/C:/mavenRepo3/com/sun/xml/bind/jaxb-impl/2.2.11/jaxb-core.jar] 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.d.settings.DevToolsSettings - Included patterns for restart : [] 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.d.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter-[\w-]+/, /spring-boot/(bin|build|out)/, /spring-boot-starter/(bin|build|out)/, /spring-boot-devtools/(bin|build|out)/, /spring-boot-actuator/(bin|build|out)/, /spring-boot-autoconfigure/(bin|build|out)/] 2025-04-09 15:09:17 [restartedMain] INFO o.s.b.d.e.DevToolsPropertyDefaultsPostProcessor - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2025-04-09 15:09:17 [restartedMain] INFO o.s.b.d.e.DevToolsPropertyDefaultsPostProcessor - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@121d2f2e 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor' 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.u.c.s.UiApplicationContextUtils - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@21d1b2df] 2025-04-09 15:09:17 [restartedMain] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.context.MissingWebServerFactoryBeanException: No qualifying bean of type 'org.springframework.boot.web.servlet.server.ServletWebServerFactory' available: Unable to start AnnotationConfigServletWebServerApplicationContext due to missing ServletWebServerFactory bean 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Creating new Restarter for thread Thread[main,5,main] 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Immediately restarting application 2025-04-09 15:09:17 [restartedMain] DEBUG o.s.boot.devtools.restart.Restarter - Starting application com.companygroup.mls.myapp.data2reader.myServer with URLs [] 2025-04-09 15:09:18 [restartedMain] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception org.springframework.boot.web.context.MissingWebServerFactoryBeanException: No qualifying bean of type 'org.springframework.boot.web.servlet.server.ServletWebServerFactory' available: Unable to start AnnotationConfigServletWebServerApplicationContext due to missing ServletWebServerFactory bean at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:212) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:182) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.companygroup.mls.myapp.data2reader.myServer.main(myServer.java:8) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 2025-04-09 15:09:18 [restartedMain] ERROR o.s.b.d.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: Web application could not be started as there was no org.springframework.boot.web.servlet.server.ServletWebServerFactory bean defined in the context. Action: Check your application's dependencies for a supported servlet web server. Check the configured web application type.
解决方案
- 确认
spring-boot-starter-web依赖完整性:移除数据库依赖时可能误删了web starter的传递依赖,或者pom中依赖配置有误(比如错误设置了scope)。确保pom.xml中的依赖配置如下,然后执行mvn clean install重新构建项目:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
- 排查是否误引入基础starter:如果替换依赖时不小心换成了
spring-boot-starter(不带web后缀),会导致缺少web服务器的自动配置。检查pom.xml,确保依赖是spring-boot-starter-web。 - 检查自定义Web服务器配置:如果存在自定义的
ServletWebServerFactory配置类,移除数据库相关代码时可能破坏了该类的逻辑。暂时注释掉自定义配置,验证应用是否能正常启动,再逐步修复配置类。 - 验证Spring Boot版本兼容性:当前使用Java 11,建议使用对Java 11支持稳定的Spring Boot版本(如2.7.x系列),避免版本不兼容导致的依赖加载问题。
- 清理Maven缓存:日志中出现jaxb相关的文件缺失警告,可能是本地Maven缓存损坏。删除本地仓库中对应目录(如
C:\mavenRepo3\com\sun\xml\bind),重新执行依赖下载,确保依赖文件完整。
内容的提问来源于stack exchange,提问作者user30241440
相关产品推荐
相关产品推荐

