运行时找不到外部依赖(Spring、JUnit):Maven项目启动异常排查
问题描述
- Maven项目编译时类路径依赖正常,但运行Spring Boot启动类时失败
- 启动代码:
public static void main(String[] args) { SpringApplication.run(App.class, args); }
- 运行时抛出异常:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener
- 已尝试显式声明必要依赖、清理构建,外部库中存在对应依赖;当前pom.xml与另一个运行正常的项目几乎一致,pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sg</groupId> <artifactId>gp-appointment-manager</artifactId> <version>0.0.1-SNAPSHOT</version> <name>h2-database</name> <description>Final project</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.16</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>24.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
原因分析
- 版本冲突:显式声明的
spring-beans、spring-aop版本(5.3.16)与Spring Boot 2.6.4 parent管理的版本不一致,导致类加载时出现兼容性问题。 - 运行时类路径异常:编译时依赖存在,但打包或运行时的类路径可能缺少
EventPublishingRunListener依赖的核心Spring Boot组件,或依赖被意外排除。 - DevTools类加载冲突:spring-boot-devtools的类加载机制特殊,可能在IDE运行或热重启时引发类加载异常。
解决方法
- 移除显式声明的Spring核心依赖:删除pom.xml中
spring-beans和spring-aop的依赖,依赖版本由Spring Boot parent自动管理,避免冲突:<!-- 删除以下两个依赖 --> <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.16</version> </dependency> --> - 清理重建项目:执行Maven命令清理缓存并重新构建:
mvn clean install - 检查运行时类路径:IDE运行时确认配置的类路径包含所有Spring Boot依赖;jar包运行时使用
java -jar命令,确保是Spring Boot插件打包的可执行jar。 - 临时禁用DevTools:若怀疑DevTools导致问题,注释掉该依赖后重新构建启动:
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> --> - 排查依赖树:使用Maven命令查看依赖传递情况,确认是否存在冲突或缺失:
mvn dependency:tree
内容的提问来源于stack exchange,提问作者user24321809
相关产品推荐
相关产品推荐

