Springboot @PropertySource 启动时访问多个属性文件失败

问题描述 投票:0回答:1

我正在使用 springboot 2.7,我尝试使用以下方法加载 2 个 yml 文件:

@PropertySource({"classpath:application.yml", "classpath:application-${spring.profiles.active}.yml"})

当我启动服务器时,出现此错误:

原因:java.io.FileNotFoundException:类路径资源[application-default,dev.yml]无法打开,因为它不存在

为什么 “classpath:application-${spring.profiles.active}.yml” --> dev.yml 而不是 application-dev.yml

java spring-boot yaml
1个回答
0
投票

因为

spring.profiles.active
属性的值为
default,dev
。因此,表达式
classpath:application-${spring.profiles.active}.yml
被 Spring 计算为
application-default,dev.yml
。如果您指定了 dev 配置文件,则无需另外使用
@PropertySource
来加载
application-dev.yml
,Spring 会自动执行此操作,就像加载
application.yml
一样。

请参阅 Spring Boot 参考

© www.soinside.com 2019 - 2024. All rights reserved.