Spring 启动 ClassNotFoundException org.springframework.core.metrics.ApplicationStartup

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

我目前正在 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 引导无法启动

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

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 文档中看到 class 定义 here,但我不确定它存在于什么依赖项中。

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

我在 findjar.com 中进行了搜索,但没有成功。

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


更新:

我补充了:

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

这给了我一个新的错误:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

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

The following method did not exist:

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

The method's class, org.springframework.context.ConfigurableApplicationContext, is available from the following locations:

... Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.context.ConfigurableApplicationContext

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

<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 而不是由于我 运行 遇到的其他问题而更新的任何东西。

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

确保你的类路径中没有旧库的记忆。

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

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

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

查看 Twitter 线程 here,感谢 Stéphane 和 Arnaud 的帮助。

我没有 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 并解决了问题。

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

    <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 中导入的 spring-security-ldap 引起的。4.x

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

我有一个类似的问题,通过添加以下 maven 依赖项解决了这个问题:

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

我第一次向库添加 Spring 启动测试时遇到了这个问题。就我而言,图书馆没有 Spring 引导应用程序。

这成功了:

@SpringBootApplication
public class TestApplication {
  public static void main(final String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

仅使用 spring-boot-starter-test 作为 Spring 依赖项。