Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup

Posted

技术标签:

【中文标题】Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup【英文标题】: 【发布时间】:2021-03-10 18:01:33 【问题描述】:

我目前正在 Spring BootGCP 数据存储 中进行一些概念验证工作。

我的pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.4.0</version>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-data-datastore</artifactId>
  <version>1.2.6.RELEASE</version>
</dependency>

问题:Spring Boot 无法启动

当我尝试启动应用程序时,我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:251)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:264)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)

我尝试了什么

我尝试添加 Actuator 依赖项。 但这并没有奏效。 我无法弄清楚我缺少什么依赖项。我在 5.3.0-M2 文档中看到了类定义 here,但我不确定它存在于什么依赖项中。

我还尝试添加以下指标依赖项:

spring-cloud-gcp-starter-metrics 弹簧指标 spring-cloud-stream-metrics

我在findjar.com 中搜索但没有运气。

如果可能的话,我也不介意禁用它。


更新:

我补充说:

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

这给了我一个新错误:

试图调用一个不存在的方法。尝试 是从以下位置制作的:

org.springframework.boot.SpringApplication.run(SpringApplication.java:324)

以下方法不存在:

'void org.springframework.context.ConfigurableApplicationContext.setApplicationStartup(org.springframework.core.metrics.ApplicationStartup)'

方法的类, org.springframework.context.ConfigurableApplicationContext,是 可从以下位置获得:

... 行动:

更正应用程序的类路径,使其包含 单一的,兼容的版本 org.springframework.context.ConfigurableApplicationContext

【问题讨论】:

你引入了对org.springframework.spring.context的依赖了吗? (关于第二个错误) 【参考方案1】:

我可以通过降级 Spring Boot 来解决这个问题:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.3.3.RELEASE</version>
</dependency>

猜它只是不兼容 2.4.0。

具体来说,我还必须确保我使用的是 2.3.3.RELEASE,而不是因为遇到其他问题而更新的版本。

【讨论】:

【参考方案2】:

我没有 spring-cloud 依赖,但是当从 2.3 升级到 Spring Boot 2.4 时,我遇到了同样的错误。

Exception in thread "main" java.lang.NoClassDefFoundError:
 org/springframework/core/metrics/ApplicationStartup

此线程 (https://github.com/spring-projects/spring-boot/issues/24880) 中的每个对话我都升级到 Spring Framework 5.3.3 并解决了问题。

【讨论】:

【参考方案3】:

由于加载了旧版本的 spring-context JAR 文件而不是适当的版本,我遇到了同样的问题。

确保您的类路径中没有旧库的回忆。

【讨论】:

【参考方案4】:

我遇到了同样的问题,但是:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.9.RELEASE</version>
        <scope>test</scope>
    </dependency>

我升级到后错误消失了:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.3.3</version>
        <scope>test</scope>
    </dependency>

【讨论】:

【参考方案5】:

我遇到了类似的问题,通过添加以下maven依赖解决了:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.3.8</version>
</dependency>

【讨论】:

【参考方案6】:

希望对您有所帮助,将 spring-cloud 升级到最新版本。

当我遇到您的第一个错误时,这对我有用:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup

请参阅推特帖子 here,感谢 Stéphane 和 Arnaud 的帮助。

【讨论】:

【参考方案7】:

我遇到了同样的问题,但这是由我也在 5.4.x 版本中导入的 spring-security-ldap 引起的

将 spring-security-ldap 升级到 5.5.0 版解决了我的问题。

【讨论】:

以上是关于Spring Boot ClassNotFoundException org.springframework.core.metrics.ApplicationStartup的主要内容,如果未能解决你的问题,请参考以下文章

使用 spring-data-jpa 和完整的 tomcat-jdbc 配置的 spring boot 为 ResetAbandonedTimer 抛出 java.lang.ClassNotFound

Maven的classNotFound错误?

从 apache Spark 运行 java 程序时出现 ClassNotFound 异常

安装 Sonata ClassificationBundle 自动加载 ClassNotFound

尝试使用 DataflowRunner 时出现 ClassNotFound 异常

ClassNotFound : 创建实体管理器工厂时的 javax.persistence.Persistence