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

Spring Boot1.3升级2.1运行时提示找不到classpath下config/foo.yml文件

Spring Boot 1.3升2.1 yml配置文件找不到问题排查方案
  • 确认编译后资源是否正确拷贝
    执行mvn clean package后,检查target/classes目录下是否存在config/foo.yml文件。如果不存在,说明Maven资源配置有问题:

检查pom.xml里的<resources>配置,确认没有排除.yml后缀文件,且src/main/resources被标记为资源目录。Spring Boot 2.x默认的spring-boot-starter-parent会自动处理yml资源,如果你自定义了resources规则,需要补充yml文件的包含配置。

  • 检查IDE运行时的资源目录配置
    如果是本地IDE运行项目,确认src/main/resources已被标记为项目的资源根目录,IDE会在运行时将该目录加入classpath。
  • 调整@PropertySource路径配置
    如果是多模块项目,foo.yml存放在依赖的子模块jar包中,需要将路径前缀从classpath:/改为classpath*:/,修改后注解如下:
@PropertySource(value = "classpath*:/config/foo.yml", factory = YamlPropertySourceFactory.class)
  • 验证YamlPropertySourceFactory实现兼容性
    Spring Boot 2.x对Yaml解析的API有调整,确认你的自定义工厂类适配2.1版本API,正确的实现参考如下:
public class YamlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        Properties properties = factory.getObject();
        return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
    }
}
  • 检查配置类扫描规则
    确认FooProperties类所在的包在Spring Boot启动类的@ComponentScan扫描范围内,或者在启动类上添加@EnableConfigurationProperties(FooProperties.class)显式注册配置属性类。

注意:你当前配置类中属性名为errorCode,但yml中配置的是config.name,后续文件加载问题解决后,需要调整属性名对应才能正常注入值。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 10:06:02