【问题标题】:java.io.FileNotFoundException: class path resource [config/foo.yml] cannot be opened because it does not existjava.io.FileNotFoundException:类路径资源 [config/foo.yml] 无法打开,因为它不存在
【发布时间】:2021-07-15 05:34:21
【问题描述】:

我正在将我的 Spring Boot 应用程序从 1.3 升级到 2.1,我能够执行 mvn compile 并构建成功。

但在运行应用程序时出现以下错误

Caused by: java.io.FileNotFoundException: class path resource [config/foo.yml] cannot be opened because it does not exist

我的配置属性类

@Configuration
@PropertySource(value = "classpath:/config/foo.yml", factory = YamlPropertySourceFactory.class)
@ConfigurationProperties(prefix = "config")
public class FooProperties {

    private String errorCode;

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }
}

foo.yml 仅存在于 src/main/resources/config/foo.yml 下且 yml 文件名正确

foo.yml 内容

config:
  name: foo

我看到了很多与相同错误相关的帖子,但对解决此问题没有帮助。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    尝试在classpath之后添加*,让它可以搜索更多的位置,所以把它改成

    @PropertySource(value = "classpath*:/config/foo.yml", factory = YamlPropertySourceFactory.class)
    

    @PropertySource(value = "classpath*:config/foo.yml", factory = YamlPropertySourceFactory.class)
    

    【讨论】:

    • @LearnToCode 你把src/main/resources 做成资源文件夹了吗?
    • 我正在使用 intelliJ,我看到 src/main/resources 文件夹标记为 Resources Root
    • 我尝试使用这个@ImportResource("classpath*:/config/foo.yml") 问题得到了解决
    • @LearnToCode 非常适合您解决问题
    • 感谢以上建议
    猜你喜欢
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2019-09-05
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多