【问题标题】:how to tell spring to load resources from external folder?如何告诉spring从外部文件夹加载资源?
【发布时间】:2019-09-07 11:07:27
【问题描述】:

我正在尝试使用 MyConfig.class.getResourceAsStream(/config/test.txt) 读取配置文件夹中的文本文件 - Spring Boot 配置类中的 MyProject/config。

MyProject
  -config
    -test.txt
  -src
    -main
      -java
        -com
          -myproject
            -configurations
              -MyConfig

它总是返回 null 作为未找到的资源。而它可以读取位于 MyProject/src/main/resources 文件夹下的文件。

MyProject
  -src
    -main
      -resources
        -test.txt
      -java
        -com
          -myproject
            -configurations
                -MyConfig

是否需要任何特定的配置来告诉spring-boot检查项目的整个路径?

@Configuration
@EnableAutoConfiguration
public class MyConfig {

  @Bean
  public Config config() { 
    return new Config(MyConfig.class.getResourceAsStream("/config/test.txt"); // This doesn't work. The same setup work if I have the file under resources folder.MyConfig.class.getResourceAsStream("/test.txt")
  }
  }

【问题讨论】:

标签: java spring-boot


【解决方案1】:

该方法将仅从类路径/模块路径加载。检查配置文件夹是否在类路径中。

【讨论】:

    【解决方案2】:

    假设您有一个典型的项目结构,请尝试将 test.txt 文件放在 src/main/resources 文件夹中,以便被 springboot 上下文扫描。

    【讨论】:

      【解决方案3】:

      例如 E:\MyProject\config\test.txt

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-17
        • 2015-11-27
        • 2013-03-22
        • 1970-01-01
        • 2011-12-23
        相关资源
        最近更新 更多