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

Spring实验室跨模块访问SQL脚本遇FileNotFoundException问题

解决Spring配置实验中无法找到common模块SQL脚本的问题

问题分析

你遇到的FileNotFoundException是因为当前12-javaconfig-dependency-injection模块未依赖00-rewards-common模块,导致Spring类路径中不包含common模块下的SQL脚本文件。从目录结构可见,SQL文件存放在00-rewards-common/src/main/resources/rewards/testdb,但该模块的资源并未被纳入当前测试模块的类路径。

解决方案

步骤1:确保common模块正确编译并安装

  1. 进入00-rewards-common目录,执行Maven命令:
mvn clean install

该命令会编译common模块,将包括SQL脚本在内的资源文件打包并安装到本地Maven仓库,供其他模块引用。
2. 验证00-rewards-common/target/classes目录下是否存在rewards/testdb/schema.sql和data.sql,若不存在,执行mvn clean compile重新编译,确保资源文件被正确复制到编译目录。

步骤2:为当前模块添加common模块依赖

修改12-javaconfig-dependency-injection模块的pom.xml,添加对common模块的依赖:

<dependencies>
    <!-- 保留原有依赖,新增以下内容 -->
    <dependency>
        <groupId>io.spring.training.core-spring</groupId> <!-- 与common模块pom.xml中的groupId一致 -->
        <artifactId>00-rewards-common</artifactId>
        <version>1.0.0-SNAPSHOT</version> <!-- 与common模块pom.xml中的version一致 -->
    </dependency>
</dependencies>

步骤3:IDE环境同步(若使用IDE开发)

  • Eclipse:右键12-javaconfig-dependency-injection项目 → Build Path → Add Projects,选中00-rewards-common添加为依赖项目,再执行Project → Clean清理并重新编译。
  • IntelliJ IDEA:打开项目结构(快捷键Ctrl+Alt+Shift+S),在Modules中找到12-javaconfig-dependency-injection,切换到Dependencies标签,点击+添加00-rewards-common模块作为依赖。

验证

重新运行RewardNetworkTests测试类,Spring即可正常加载common模块中的SQL脚本,不再抛出文件找不到的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 06:15:10