【问题标题】:After upgrading spring version getting java.lang.NoClassDefFoundError:升级spring版本后出现java.lang.NoClassDefFoundError:
【发布时间】:2020-04-23 08:58:53
【问题描述】:

我正在尝试在我们拥有的多个项目中将 spring 从 2.1.1 升级到 2.2.0。 我已经在几个项目中完成了,一切都很顺利。

在当前项目中,我做了同样的改变:

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

<properties>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
        <spring.boot.version>2.2.0.RELEASE</spring.boot.version>
    ...
    ...

</properties>

<dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
        .....
        .....
</dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
...
</dependencies>

现在,当我尝试启动我的应用时,出现以下错误:

Caused by: java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy

我检查并根据文档,这个类应该在我当前的 spring-core 版本中可用: https://docs.spring.io/spring/docs/5.2.0.RELEASE/javadoc-api/org/springframework/core/annotation/MergedAnnotations.SearchStrategy.html

堆栈跟踪:

java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy
        at org.springframework.boot.BeanDefinitionLoader.isComponent(BeanDefinitionLoader.java:279) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:156) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:136) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:128) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.SpringApplication.load(SpringApplication.java:691) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:392) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:197) ~[spring-cloud-context-2.0.0.RELEASE.jar:2.0.0.RELEASE]
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:104) ~[spring-cloud-context-2.0.0.RELEASE.jar:2.0.0.RELEASE]
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:70) ~[spring-cloud-context-2.0.0.RELEASE.jar:2.0.0.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:152) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:132) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:92) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:171) [spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]

maven 依赖树,所有 spring 事件:

[INFO] +- org.springframework.security:spring-security-config:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-config:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-core:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-core:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-context:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-context:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-web:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework.security:spring-security-web:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.0.RELEASE:provided
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.0.RELEASE:provided
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:5.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jdbc:jar:5.2.0.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:5.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-orm:jar:5.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:5.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-tx:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:5.2.0.RELEASE:compile
[INFO] +- org.apache.cxf:cxf-spring-boot-starter-jaxrs:jar:3.2.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.apache.cxf:cxf-spring-boot-autoconfigure:jar:3.2.5:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-actuator-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:2.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-actuator:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.2.0.RELEASE:test
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.2.0.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.2.0.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:2.2.0.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.0.RELEASE:test
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.0.RELEASE:test
[INFO] |  +- org.springframework:spring-test:jar:5.2.0.RELEASE:test
[INFO] |  +- org.springframework:spring-test:jar:5.2.0.RELEASE:test
[INFO] +- org.springframework.cloud:spring-cloud-config-client:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.cloud:spring-cloud-config-client:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-commons:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-commons:jar:2.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework.security:spring-security-crypto:jar:5.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework.security:spring-security-crypto:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework.cloud:spring-cloud-context:jar:2.2.0.RELEASE:compile
[INFO] |  \- org.springframework.cloud:spring-cloud-context:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.retry:spring-retry:jar:1.2.4.RELEASE:compile
[INFO] +- org.springframework.retry:spring-retry:jar:1.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:5.2.0.RELEASE:compile
[INFO] +- org.springframework:spring-core:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-jcl:jar:5.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-jcl:jar:5.2.0.RELEASE:compile
[INFO]    \- org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.2.0.RELEASE:compile
[INFO]    \- org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.2.0.RELEASE:compile
[INFO]       +- org.springframework.cloud:spring-cloud-starter:jar:2.2.0.RELEASE:compile
[INFO]       +- org.springframework.cloud:spring-cloud-starter:jar:2.2.0.RELEASE:compile
[INFO]       |  \- org.springframework.security:spring-security-rsa:jar:1.0.7.RELEASE:compile
[INFO]       |  \- org.springframework.security:spring-security-rsa:jar:1.0.7.RELEASE:compile
[INFO]       +- org.springframework.cloud:spring-cloud-openfeign-core:jar:2.2.0.RELEASE:compile
[INFO]       +- org.springframework.cloud:spring-cloud-openfeign-core:jar:2.2.0.RELEASE:compile
[INFO]       |  +- org.springframework.cloud:spring-cloud-netflix-ribbon:jar:2.2.0.RELEASE:compile
[INFO]       |  +- org.springframework.cloud:spring-cloud-netflix-ribbon:jar:2.2.0.RELEASE:compile
[INFO]       |  |  \- org.springframework.cloud:spring-cloud-netflix-archaius:jar:2.2.0.RELEASE:compile
[INFO]       |  |  \- org.springframework.cloud:spring-cloud-netflix-archaius:jar:2.2.0.RELEASE:compile
[INFO]       |  \- io.github.openfeign.form:feign-form-spring:jar:3.8.0:compile

【问题讨论】:

  • 使用 Spring Cloud 时不能简单地升级 Spring Boot。您还必须将 Spring Cloud 升级到支持该 Spring Boot 版本的版本。较旧的 Spring Cloud 依赖项会引入旧的 Spring 依赖项,因此会出现异常。
  • @M.Deinum - 我尝试将 spring-cloud 升级到 Hoxton 版本,但没有帮助
  • 您不需要 spring-boot-dependencies,因为您添加的父级已经暗示了这一点。如果 Spring Cloud 的升级不起作用,则必须在旧版本的 Spring 中引入另一个依赖项(或者您自己做了)。使用mvn dependency:tree 找出导致旧版 Spring 的原因。
  • @M.Deinum 在依赖树中,我应该搜索旧版本的 spring.framework.core 吗?我添加了依赖树,但是我没有找到任何旧版本的spring framework.core
  • 您的依赖关系树可能有问题……您的 IDE 可能会使问题更加复杂。修复 cmets 中规定的依赖关系,清除 IDE 缓存(如果有),退出 IDE,删除“目标”目录,然后再次启动 IDE 并进行完整编译。

标签: java spring pom.xml


【解决方案1】:

参考:Documentation

当作为 Spring IO 平台一部分的工件声明时没有版本,将使用依赖项的预定义版本,对于 spring-core,它是

org.springframework spring-core 5.0.13.RELEASE

https://docs.spring.io/spring/docs/5.0.13.RELEASE/javadoc-api/org/springframework/core/annotation/ 缺少 MergedAnnotations 类。

使用您选择的版本声明依赖项

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>

更新(我之前的回答似乎有点误导)

不需要明确声明版本。文档的Dependency Management 部分提到了以下内容。

Spring Boot 的每个版本都提供了一个精选的依赖项列表 它支持。在实践中,您不需要提供版本 对于构建配置中的任何这些依赖项,如 Spring 引导为您管理。当你升级 Spring Boot 本身时,这些 依赖项也以一致的方式升级。

另外,在继承 starter parent 时需要的升级修改说明here

【讨论】:

  • 请检查作为 Maven 依赖项添加的 jar 以验证版本
  • 添加的jar是5.2.0版本的
  • 来自spring.io/projects/spring-cloud Release train Finchley 与 Spring boot 版本 2.0.x 兼容。尝试将其升级到 Hoxton 以与 Spring Boot 版本 2.2.x 兼容
猜你喜欢
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多