You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot从2.2.4升级至2.7.2:mongoDatabaseFactory Bean创建失败求助

问题描述

现有Spring Boot应用,当前父依赖版本:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.4.RELEASE</version>
</parent>

升级父依赖到2.7.2.RELEASE后,启动报错:

ERROR [main] TomcatStarter:61 onStartup - Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'beanConfiguration': Unsatisfied dependency expressed through field 'tokenServices'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TokenServices': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authProfileImpl': Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'mongoDatabaseFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
排查步骤

核心错误是NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory,该类属于MongoDB Java驱动,按以下步骤排查:

  • 检查MongoDB依赖版本兼容性:Spring Boot 2.7.x默认管理的MongoDB驱动版本为4.4.x系列,而2.2.4对应3.11.x系列。若pom.xml中手动指定了mongodb-driver、mongodb-driver-sync或spring-boot-starter-data-mongodb的版本,直接删除手动指定的版本号,让父依赖统一管理版本。

  • 分析依赖树找冲突:执行Maven命令mvn dependency:tree(Gradle项目用./gradlew dependencies),搜索mongodb关键词,查看是否存在多版本驱动依赖。若有旧版本依赖被间接引入,在对应依赖节点添加<exclusions>排除旧版本。

  • 清理缓存重新构建:本地仓库依赖缓存可能损坏,执行mvn clean install -U(强制更新快照依赖)或./gradlew clean build --refresh-dependencies,清理后重新构建项目。

  • 检查自定义Mongo配置:若项目中有自定义Mongo配置类(如手动创建MongoClient或MongoTemplate),检查是否使用了Spring Boot 2.7.x中已废弃或移除的API。例如旧版本的MongoClientOptions已被MongoClientSettings替代,需调整配置逻辑适配新版本。

  • 确认starter依赖引入:确保项目引入的是spring-boot-starter-data-mongodb而非单独的mongodb-driver依赖,starter会自动管理所有相关依赖(驱动、Spring Data MongoDB等)的版本兼容性,避免手动引入导致的版本冲突。

内容的提问来源于stack exchange,提问作者Sahil Khanna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 09:25:14