Spring Boot - 从 2.2.5 升级到 2.5.7 后,应用程序无法启动

Posted

技术标签:

【中文标题】Spring Boot - 从 2.2.5 升级到 2.5.7 后,应用程序无法启动【英文标题】:Spring Boot - After upgrading from 2.2.5 to 2.5.7, application failed to start 【发布时间】:2022-01-23 00:24:59 【问题描述】:

我将 spring boot 从 2.2.5 升级到 2.5.7,我正在使用 MongoDB。由于编译失败,我删除了 spring-boot-starter-mongo 并在下面添加了解决我的编译问题的依赖项

<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongodb-driver</artifactId>
  <version>3.11.2</version>    
</dependency>
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongodb-driver-sync</artifactId>
  <version>3.11.0</version>
</dependency>
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>2.2.5.RELEASE</version>

如果我运行应用程序,我将面临上述所有更改 java.lang.ClassNotFoundException: org.springframework.data.mongodb.MongoDatabaseFactory

同时添加运行时日志:

需要输入来解决此问题

2021-12-21 20:16:00.263 ERROR [ht-ui-api : ] 19004 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed
java.lang.IllegalArgumentException: Could not find class [org.springframework.data.mongodb.MongoDatabaseFactory]
    at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:334) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.core.annotation.TypeMappedAnnotation.adapt(TypeMappedAnnotation.java:446) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.core.annotation.TypeMappedAnnotation.getValue(TypeMappedAnnotation.java:369) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.core.annotation.TypeMappedAnnotation.asMap(TypeMappedAnnotation.java:284) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.core.annotation.AbstractMergedAnnotation.asAnnotationAttributes(AbstractMergedAnnotation.java:193) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.core.type.AnnotatedTypeMetadata.getAnnotationAttributes(AnnotatedTypeMetadata.java:106) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.AnnotationConfigUtils.attributesFor(AnnotationConfigUtils.java:285) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.AnnotationBeanNameGenerator.determineBeanNameFromAnnotation(AnnotationBeanNameGenerator.java:102) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.AnnotationBeanNameGenerator.generateBeanName(AnnotationBeanNameGenerator.java:81) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.registerBeanDefinitionForImportedConfigurationClass(ConfigurationClassBeanDefinitionReader.java:169) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:150) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:129) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:343) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.7.jar:2.5.7]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) ~[spring-boot-2.5.7.jar:2.5.7]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:445) ~[spring-boot-2.5.7.jar:2.5.7]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.7.jar:2.5.7]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354) ~[spring-boot-2.5.7.jar:2.5.7]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.7.jar:2.5.7]
    at com.htds.api.HtUiApiApplication.main(HtUiApiApplication.java:15) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.7.jar:2.5.7]
Caused by: java.lang.ClassNotFoundException: org.springframework.data.mongodb.MongoDatabaseFactory
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
    at java.base/java.lang.Class.forName(Class.java:468) ~[na:na]
    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:145) ~[spring-boot-devtools-2.5.7.jar:2.5.7]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
    at java.base/java.lang.Class.forName(Class.java:468) ~[na:na]
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:284) ~[spring-core-5.3.13.jar:5.3.13]
    at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:324) ~[spring-core-5.3.13.jar:5.3.13]
    ... 29 common frames omitted

【问题讨论】:

嗨@xerx593,当我使用 org.springframework.bootspring-boot-starter-data-mongodb它给了我编译错误找不到符号:MongoClient 并且找不到 MongoDB 各种类的符号,因此我使用了 org.springframework.dataspring-data-mongodb2.2.5.RELEASE 解决了我的编译问题。现在我面临运行时错误 这意味着:或者你将解决编译问题,或者你不会升级(spring-boot)! ;( 我希望this(offcial, correct version(s) upgrade guide) 有所帮助! 如果你(今天)升级 spring-boot 然后“try all-in”:2.6.1! ;) 【参考方案1】:

为什么要“将 spring boot 升级到 2.5.7”? (为什么,今天,不是 2.6.1?;)但是你为什么离开 spring-data-mongodb:2.2.5.RELEASE

请对齐

即:确认 spring-data-mongodb 版本与 spring-boot 版本(反之亦然)!如果是2.5.7,则为:

org.mongodb:mongodb-driver-sync:4.2.3 org.springframework.data:spring-data-mongodb:3.2.7

...proof for both。 #


升级spring-data-mongodb(要不要!2.x -&gt; 3.x,(升级到spring-boot:2.XXX显然需要这个),请参考Reference Documentation#upgrading.2-3。

希望也有帮助:(latest) Spring Boot Release Notes(带有指向以前版本的快速链接,Ctrl+Fmongo... data;)

【讨论】:

以上是关于Spring Boot - 从 2.2.5 升级到 2.5.7 后,应用程序无法启动的主要内容,如果未能解决你的问题,请参考以下文章

更新到 Spring Boot 2 后 Jackson 模块未注册

从Spring Boot 1.5升级到2.0

将 Spring Boot 从 2.4.X 升级到 2.6.X 后无法运行我的 jar

Spring Boot 版本从 2.1.6 升级到 2.2.1 和 spring-cloud 问题

将 Spring Boot 项目从 JDK 8 升级到 JDK 11

独立资源服务器(Spring Boot 2 + OAuth + JWT)在 Spring-boot 从 1.2.x 升级到 2.x 后给出 UsernameNotFoundException