【问题标题】:"NoClassDefFoundError: GenericApplicationListener" when switching from Spring Boot 1.3.0M1 to 1.3.0M2从 Spring Boot 1.3.0M1 切换到 1.3.0M2 时出现“NoClassDefFoundError: GenericApplicationListener”
【发布时间】:2015-10-08 00:12:18
【问题描述】:

在我的 Spring Starter 创建的 Soring Boot 应用程序中,我从 Spring Boot 1.3.0M1 切换到 1.3.0M2。尝试启动应用程序时(从 STS 3.7.0 或通过 spring-boot:run 在命令行)我得到以下异常:

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:435)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.logging.ClasspathLoggingApplicationListener
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385)
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:362)
        at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:231)
        at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:206)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:971)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:960)
        at com.daimler.daivb.services.application.dummyservice.DummyServiceApplication.main(DummyServiceApplication.java:18)
        ... 6 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
        at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:378)
        ... 12 more
Caused by: java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 25 more

提到的类已添加到 spring-context 版本 4.2.0 但查看依赖树时,我可以看到 Spring Boot 1.3.0M2 仍然依赖于 spring-context 4.1.6:

[INFO] com.company.services.application:dummyservice:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.3.0.M2:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.3.0.M2:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.1.6.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.0.M2:compile
[INFO] |     \- org.yaml:snakeyaml:jar:1.15:compile

【问题讨论】:

  • 试图通过排除 4.1.6 传递依赖来明确地将依赖添加到 spring-context 4.2.0.RC3,导致以下错误:Caused by: java.lang.NoSuchMethodError: org.springframework.beans.factory.config.ConfigurableBeanFactory.getSingletonMutex()Ljava/lang/Object;
  • Spring Boot 4.1 应该使用 Spring 4.2 确保你没有在你的 pom 中指定 spring.version 属性或有其他一些强制 4.1.6 版本的依赖项。
  • 我遇到了同样的事情。我更新了我的 Spring Cloud 应用程序以使用 Spring Boot 1.3.0.M5 版本,它似乎对 Spring Framework 4.1.6 有大量依赖项。在依赖关系树中,我注意到一些是 4.2.1,但大多数仍然是 4.1.6。我在任何地方都没有明确的 4.1.6 spring.version
  • 我通过使用 -U 运行构建来更新所有快照依赖项来解决这个问题。
  • 我遇到了类似的问题,在我的 build.gradle 中将我的 spring 版本从 4.1.6 更改为 4.2.1 解决了它。

标签: spring spring-boot


【解决方案1】:
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener

根据上述错误,应用程序无法找到从 jar 依赖项中定义的类。 GenericApplicationListener 是从 Spring 4.2 版添加的。

将您的 Spring 版本升级到 4.2 并重新检查。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,因为我的 POM 有一个属性部分,其中包含从另一个项目复制的 &lt;spring.version&gt; 标记。

    删除属性部分中可能与 Spring Boot 库冲突的任何 &lt;abc.version&gt; 标记。

    【讨论】:

      【解决方案3】:

      Spring Boot 为您和allows you to add them without specifying the version 管理一堆依赖项。 Spring Boot 显然是基于 Spring Framework 并使用最新最好的,因此您应该不惜一切代价避免修复 Spring Framework 版本以供自己使用。

      如果您已升级到较新版本(包括错误修复或其他内容),请确保在升级 Spring Boot 之前删除覆盖,因为它所需的新版本可能与您的覆盖不兼容.

      如果您正在使用在 Spring Framework 上提供依赖管理的其他项目(例如 Spring Cloud),请确保优先使用 Spring Boot 的依赖管理。使用 Maven,确保将 spring-boot-dependencies BOM 添加为 the first entry in the &lt;dependenciesManagement&gt; section

      【讨论】:

      • 我也会选择这个答案。我的 pom.xml 对 spring-test-mvc 和 spring-test 有两个不同的依赖项声明,这显然导致类加载混乱。删除 spring-test-mvc 为我解决了这个问题。
      • 上次我遇到这个问题,从 Spring-Boot 版本 1.3.7 更新到 1.3.8 解决了这个问题。
      猜你喜欢
      • 2019-11-04
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 2020-08-19
      • 2023-02-14
      • 2016-03-07
      相关资源
      最近更新 更多