在 Spring Boot 的子模块中添加 Resource 目录,可以按照以下步骤进行操作:

  1. 在子模块的根目录下创建一个新的目录,命名为'src/main/resources'。

  2. 将需要添加到 Resource 目录中的文件或文件夹拷贝到新创建的'src/main/resources'目录下。

  3. 打开子模块的 pom.xml 文件,在标签下添加如下配置:

<resources>
  <resource>
    <directory>src/main/resources</directory>
  </resource>
</resources>

这样,Maven 在构建子模块时就会将'src/main/resources'目录下的文件或文件夹打包到生成的 Jar 或 War 文件中。

  1. 在 Spring Boot 应用程序中使用 Resource 文件,可以使用以下方式:
@Autowired
ResourceLoader resourceLoader;

public void loadResource() {
   Resource resource = resourceLoader.getResource('classpath:file.txt');
   // 使用 Resource 对象读取文件内容
}

其中,'classpath:' 表示在类路径下查找文件,可以使用其他前缀来指定不同的资源路径。

Spring Boot 子模块添加 Resource 目录指南

原文地址: https://www.cveoy.top/t/topic/lylN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录