You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot测试报错:依赖注入失败与类缺失问题求助

问题排查与解决方案

核心原因分析

java.lang.ClassNotFoundException: org.springframework.data.jpa.repository.support.SimpleJpaRepository$AjcClosure79 是AspectJ织入过程中生成的动态类无法被类加载器识别导致的,本质是项目AspectJ依赖、配置与Spring Boot版本不兼容或织入配置缺失。

具体排查与修复步骤

  • 校验AspectJ依赖版本兼容性
    确保spring-boot-starter-aop、aspectjweaver、aspectjrt的版本与Spring Boot版本匹配:Spring Boot 2.x对应AspectJ 1.9.x系列,Spring Boot 3.x需使用AspectJ 1.10.x及以上版本。
  • 检查AspectJ编译时织入(CTW)配置
    若用Maven编译,需确认aspectj-maven-plugin已正确配置,插件版本要与AspectJ依赖版本一致,同时在插件的weaveDependencies中包含Spring Data JPA相关依赖。
  • 验证测试类配置
    测试类需添加@SpringBootTest注解,确保配置能正确加载AspectJ相关逻辑;检查测试类路径是否包含AspectJ编译生成的产物(如target/classes下的带Ajc后缀类文件)。
  • 排除依赖冲突
    执行mvn dependency:tree命令查看依赖树,排除重复或版本不兼容的AspectJ、Spring Data JPA依赖。

参考Maven配置示例

<dependencies>
    <!-- Spring Boot AOP 启动依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!-- AspectJ Weaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.14.0</version> <!-- 适配你的Spring Boot版本 -->
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <complianceLevel>17</complianceLevel> <!-- 对应项目使用的JDK版本 -->
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.9.9.1</version> <!-- 与aspectjweaver版本保持一致 -->
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

额外检查项

  • 若使用IntelliJ IDEA,需开启AspectJ编译支持:依次进入File → Settings → Build, Execution, Deployment → Compiler → Java Compiler,勾选"Enable annotation processing",同时确认AspectJ插件已安装。
  • 执行mvn clean install清理并重新构建项目,确保编译产物正确生成并被加载。

内容的提问来源于stack exchange,提问作者Dglasmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:22:12