Spring Boot Gradle应用运行报错:无法解析占位符,外部配置如何引用主配置变量?
Spring Boot外部配置文件引用application.properties变量的问题解决
问题场景
运行Spring Boot Gradle应用时,外部配置文件无法引用application.properties中的变量,抛出占位符无法解析的错误。
报错信息
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'default.user' in value "${default.user}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-5.3.24.jar:5.3.24] at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-5.3.24.jar:5.3.24] at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-5.3.24.jar:5.3.24] at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-5.3.24.jar:5.3.24] at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:191) ~[spring-context-5.3.24.jar:5.3.24] at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1332) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.24.jar:5.3.24] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.24.jar:5.3.24] ... 17 common frames omitted
相关配置文件
src/main/resources/application.properties
app.user=testaccount
src/main/resources/config/testing.properties
default.user=${app.user}
代码片段
@Value("${default.user}") private String defaultUser;
已尝试的无效配置
在application.properties中添加以下配置,但问题未解决:
spring.config.location=/config/testing.properties spring.config.additional-location=/config/testing.properties
解决方案
1. 修正配置文件加载路径
使用spring.config.additional-location时,需指定正确的类路径路径(testing.properties位于src/main/resources/config下),正确配置应为:
spring.config.additional-location=classpath:/config/testing.properties
注意:spring.config.location会覆盖Spring Boot默认配置文件加载路径,导致application.properties无法被加载,因此必须用spring.config.additional-location追加配置文件,而非替换。
2. 通过@PropertySource注解加载配置
在配置类上添加@PropertySource注解显式加载外部配置文件,Spring会自动利用已加载的application.properties变量解析占位符:
@Configuration @PropertySource(value = "classpath:/config/testing.properties", encoding = "UTF-8") public class AppConfig { }
3. 验证配置加载顺序
启动应用时,查看日志中包含Loading property source的条目,确认application.properties和testing.properties均被加载,且application.properties先于外部配置文件加载,确保占位符能被正确解析。
内容的提问来源于stack exchange,提问作者Lee
相关产品推荐
相关产品推荐

