java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener ----g...

 

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. GenericApplicationListener is added from spring version 4.2.

Upgrade your spring version to 4.2 and recheck.

http://stackoverflow.com/questions/31489947/noclassdeffounderror-genericapplicationlistener-when-switching-from-spring-bo

 

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.

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

If you are using other projects that also provide 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-dependencies BOM as the first entry in the <dependenciesManagement> section.

http://stackoverflow.com/questions/31489947/noclassdeffounderror-genericapplicationlistener-when-switching-from-spring-bo

 

13.2 Maven

Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:

  • Java 1.6 as the default compiler level.
  • UTF-8 source encoding.
  • Dependency Management section, allowing you to omit <version> tags for common dependencies, inherited from the spring-boot-dependencies POM.
  • Sensible resource filtering.
  • Sensible plugin configuration (exec pluginsurefireGit commit IDshade).
  • Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties andapplication-foo.yml)

On the last point: since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).

13.2.1 Inheriting the starter parent

To configure your project to inherit from the spring-boot-starter-parent simply set the parent:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent>
[Note]

You should only need to specify the Spring Boot version number on this dependency. If you import additional starters, you can safely omit the version number.

With that setup, you can also override individual dependencies by overriding a property in your own project. For instance, to upgrade to another Spring Data release train you’d add the following to your pom.xml.

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
[Tip]

Check the spring-boot-dependencies pom for a list of supported properties.

13.2.2 Using Spring Boot without the parent POM

Not everyone likes inheriting from the spring-boot-starter-parent POM. You may have your own corporate standard parent that you need to use, or you may just prefer to explicitly declare all your Maven configuration.

If you don’t want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using ascope=import dependency:

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

That setup does not allow you to override individual dependencies using a property as explained above. To achieve the same result, you’d need to add an entry in thedependencyManagement of your project before the spring-boot-dependencies entry. For instance, to upgrade to another Spring Data release train you’d add the following to your pom.xml.

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-SR2</version> <scope>import</scope> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
[Note]

In the example above, we specify a BOM but any dependency type can be overridden that way.

13.2.3 Changing the Java version

The spring-boot-starter-parent chooses fairly conservative Java compatibility. If you want to follow our recommendation and use a later Java version you can add a java.version property:

<properties>
    <java.version>1.8</java.version>
</properties>

13.2.4 Using the Spring Boot Maven plugin

Spring Boot includes a Maven plugin that can package the project as an executable jar. Add the plugin to your <plugins> section if you want to use it:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
[Note]

If you use the Spring Boot starter parent pom, you only need to add the plugin, there is no need for to configure it unless you want to change the settings defined in the parent.

 

java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy at org.springframework.boot.BeanDefinitionLoader.isComponent(BeanDefinitionLoader.java:279) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:156) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:136) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:128) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.load(SpringApplication.java:691) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:392) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:203) [spring-cloud-context-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:114) [spring-cloud-context-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:71) [spring-cloud-context-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at com.retail.RetailApplication.main(RetailApplication.java:29) [classes/:?] Caused by: java.lang.ClassNotFoundException: org.springframework.core.annotation.MergedAnnotations$SearchStrategy at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_131] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_131] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[?:1.8.0_131] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_131] ... 22 more 2025-08-01 22:53:30,698|ERROR|main|org.springframework.boot.SpringApplication:826|Application run failed java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy at org.springframework.boot.BeanDefinitionLoader.isComponent(BeanDefinitionLoader.java:279) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:156) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:136) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:128) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.load(SpringApplication.java:691) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:392) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:203) ~[spring-cloud-context-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:114) ~[spring-cloud-context-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:71) ~[spring-cloud-context-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) ~[spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.13.RELEASE.jar:2.2.13.RELEASE] at com.retail.RetailApplication.main(RetailApplication.java:29) [classes/:?] Caused by: java.lang.ClassNotFoundException: org.springframework.core.annotation.MergedAnnotations$SearchStrategy at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_131] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_131] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[?:1.8.0_131] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_131] ... 22 more
最新发布
08-02
<think>我们面对的问题是在Spring Boot 2.2.13应用启动时出现`java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy`异常。根据引用信息,这个错误通常是因为Spring Boot版本过低,没有包含`MergedAnnotations`类(该类在Spring Framework 5.2中引入)[^1][^4]。 ### 问题分析 1. **错误原因**:`MergedAnnotations`是Spring Framework 5.2引入的类,而Spring Boot 2.2.x默认使用的是Spring Framework 5.2.x。但为什么会出现这个错误? - 可能的原因:项目中的某些依赖引入了更高版本的Spring Framework(例如5.3.x或6.x)的类,而Spring Boot 2.2.13自带的Spring Framework版本是5.2.13,其中`MergedAnnotations$SearchStrategy`是存在的。因此,更可能是依赖冲突导致加载了不兼容的版本。 2. **依赖冲突**:项目中可能直接或间接引入了与Spring Boot 2.2.13不兼容的第三方库,这些库依赖了更高版本的Spring Framework,导致在运行时加载了错误的类。 ### 解决方案 #### 步骤1:检查Spring Boot和Spring Framework版本 Spring Boot 2.2.13对应的Spring Framework版本是5.2.13。确保你的项目中没有覆盖Spring Framework的版本。在Maven中,可以通过`mvn dependency:tree`查看依赖树,检查是否有其他依赖引入了不同版本的Spring Framework。 #### 步骤2:排除冲突的依赖 如果发现其他依赖引入了高版本的Spring Framework,可以在依赖中排除它们。例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> </exclusions> </dependency> ``` #### 步骤3:确保使用一致的Spring版本 在Maven中,可以通过`<properties>`指定Spring Framework的版本,确保所有模块使用同一版本: ```xml <properties> <spring-framework.version>5.2.13.RELEASE</spring-framework.version> </properties> ``` 然后对于每个Spring依赖,可以不用指定版本(由Spring Boot管理),或者显式指定为上述版本。 #### 步骤4:检查是否引入了Spring Boot 3.x或Spring Framework 6.x的依赖 根据引用[4],有时错误是因为引入了高版本的Spring依赖(如spring-test 6.0.2)而Spring Boot版本较低。因此,检查依赖中是否有高版本的Spring依赖,如: ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>6.0.2</version> </dependency> ``` 应该将其版本调整为与Spring Boot 2.2.13兼容的版本(5.2.13.RELEASE)。 #### 步骤5:清理和重新构建 执行清理并重新构建项目: ```bash mvn clean install ``` ### 预防措施 - 使用依赖管理工具(如Maven的`<dependencyManagement>`)统一管理版本。 - 定期检查依赖冲突。 ### 总结 该错误通常是由于依赖冲突导致,解决的关键是确保整个项目使用一致的Spring Framework版本(5.2.13.RELEASE),并排除其他依赖引入的高版本Spring Framework。 §§相关问题§§ 1. 如何查看Maven项目的依赖树? 2. 如何解决Spring Boot中的依赖冲突? 3. Spring Boot版本与Spring Framework版本的对应关系是怎样的?</think>### Spring Boot启动时`NoClassDefFoundError: MergedAnnotations$SearchStrategy`解决方案 您遇到的`java.lang.NoClassDefFoundError: org/springframework/core/annotation/MergedAnnotations$SearchStrategy`错误是由于**Spring Boot版本与Spring Framework版本不兼容**导致的。`MergedAnnotations`类是在Spring Framework 5.2中引入的,而Spring Boot 2.2.13默认使用Spring 5.2.x版本,但依赖冲突可能导致加载了错误版本的核心库[^1][^4]。 #### 根本原因分析 1. **版本冲突**(最常见): - 项目中的其他依赖引入了不兼容的Spring Framework版本(如5.1.x或6.x) - Maven/Gradle依赖传递导致加载了错误版本的`spring-core`或`spring-context` 2. **依赖缺失**: - 未正确引入`spring-core`或`spring-context`依赖 - 依赖作用域(如`test`)配置错误 3. **构建问题**: - 未清理旧的构建缓存(如`target/`目录) - 多模块项目中子模块版本不一致 #### 解决方案步骤 ##### 1. 检查并统一Spring版本(Maven示例) 在`pom.xml`中显式声明Spring版本: ```xml <properties> <!-- Spring Boot 2.2.13对应的Spring版本 --> <spring-framework.version>5.2.13.RELEASE</spring-framework.version> </properties> <dependencies> <!-- 显式指定核心依赖版本 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> </dependencies> ``` ##### 2. 排除冲突依赖 使用`mvn dependency:tree`查找冲突依赖,然后排除旧版本: ```xml <dependency> <groupId>com.example</groupId> <artifactId>problematic-lib</artifactId> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> </exclusions> </dependency> ``` ##### 3. 清理并重建项目 ```bash # Maven mvn clean install -U # Gradle ./gradlew clean build --refresh-dependencies ``` ##### 4. 验证依赖树 检查是否所有Spring模块版本一致(5.2.13.RELEASE): ``` [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.2.13.RELEASE [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.2.13.RELEASE [INFO] | \- org.springframework.boot:spring-boot:jar:2.2.13.RELEASE [INFO] \- org.springframework:spring-webmvc:jar:5.2.13.RELEASE [INFO] \- org.springframework:spring-context:jar:5.2.13.RELEASE ✅ ``` #### 预防措施 1. 使用Spring Boot的BOM管理版本: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.13.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. 定期运行`mvn versions:display-dependency-updates`检查版本兼容性 3. 避免直接引入高版本Spring依赖(如Spring 6.x),除非升级整个Spring Boot > **关键提示**:Spring Boot 2.2.x的生命周期已于2021年8月终止,建议考虑升级到[Spring Boot 2.7+](https://spring.io/projects/spring-boot#support)获得长期支持[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值