从 Spring Boot 1.3.0M1 切换到 1.3.0M2 时出现“NoClassDefFoundError: GenericApplicationListener”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31489947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:41:44  来源:igfitidea点击:

"NoClassDefFoundError: GenericApplicationListener" when switching from Spring Boot 1.3.0M1 to 1.3.0M2

springspring-boot

提问by Michael Rumpf

In my Spring Starter created Soring Boot application I switched from Spring Boot 1.3.0M1 to 1.3.0M2. When trying to start the application (either from STS 3.7.0 or via spring-boot:run at the command line) I get the following exception:

在我的 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.access0(URLClassLoader.java:73)
        at java.net.URLClassLoader.run(URLClassLoader.java:368)
        at java.net.URLClassLoader.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

The mentioned class has been added in spring-context version 4.2.0 but when looking at the dependency tree I can see that Spring Boot 1.3.0M2 still depends on spring-context 4.1.6:

提到的类已在 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

回答by Stephane Nicoll

Spring Boot manages a bunch of dependencies for you and allows you to add them without specifying the version. Spring Boot is obviously based on Spring Framework and uses the latest and greatest so you should avoid at all cost to fix the spring framework version to use yourself.

Spring Boot 为您管理一堆依赖项,并允许您在不指定版本的情况下添加它们。Spring Boot 显然基于 Spring Framework 并使用最新最好的,因此您应该不惜一切代价避免修复 spring 框架版本以供自己使用。

If you have upgraded to a newer version (to include a bug fix or something) make sure to remove the overridebefore you upgrade Spring Boot as the new version it requires may be incompatible with your override.

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

If you are using other projects that alsoprovide a dependency management on Spring Framework (such as Spring Cloud) make sure that the dependency management of Spring Boot takes precedence. With Maven, make sure to add the spring-boot-dependenciesBOM as the first entryin the <dependenciesManagement>section.

如果您正在使用其他也在Spring Framework 上提供依赖管理的项目(例如 Spring Cloud),请确保优先使用 Spring Boot 的依赖管理。与Maven,请务必将添加spring-boot-dependencies的BOM第一个条目<dependenciesManagement>部分

回答by Agustí Sánchez

I had the same problem because my POM had a properties section with a <spring.version>tag copied from another project.

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

Remove any <abc.version>tag in your properties section that may conflict with Spring Boot libraries.

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

回答by NextMark

Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener

From the above error, application is unable to find the class defined from the jar dependencies. GenericApplicationListeneris added from spring version 4.2.

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

Upgrade your Spring version to 4.2 and recheck.

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