springboot报错无法读取配置文件:Could not resolve placeholder 'xxx' in value "${XXXX}

SpringBoot读取配置文件

在springboot项目中,通过@Value("${aliyun.oss.file.endpoint}")读取application.properties配置文件的内容报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'constantPropertiesUtils': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aliyun.oss.file.filehost' in value "${aliyun.oss.file.filehost}"
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:403) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
	at com.rwh.oss.OssApplication.main(OssApplication.java:18) [classes/:na]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'aliyun.oss.file.filehost' in value "${aliyun.oss.file.filehost}"
	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:908) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1228) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	... 17 common frames omitted

解决办法

很简单,只需要在启动类加上

	@Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {

        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();

        c.setIgnoreUnresolvablePlaceholders(true);

        return c;
    }

即可成功启动

Spring配置中出现 `Could not resolve placeholder 'h5Service.url' in value` 错误,通常表明配置中引用的占位符无法在当前的环境中解析。以下是可能导致该问题的原因及解决方法: ### 1. **检查配置文件中是否定义了占位符** 确保在 `application.yml` 或 `application.properties` 文件中定义了 `h5Service.url` 的值。例如: ```yaml h5Service: url: http://example.com ``` 如果该值未在配置文件中定义,则Spring无法解析占位符,从而导致错误。 ### 2. **确认配置文件是否被正确加载** 如果 `h5Service.url` 已经定义,但仍然报错,则可能是配置文件未被正确加载。检查Spring Boot的启动日志,确认配置文件是否被正确读取,并且 `h5Service.url` 是否被加载到环境中。 ### 3. **使用 `@PropertySource` 显式加载配置文件** 如果占位符定义在自定义的配置文件中(例如 `custom.properties`),需要通过 `@PropertySource` 注解显式加载该文件: ```java @Configuration @PropertySource("classpath:custom.properties") public class CustomConfig { } ``` 同时,确保 `custom.properties` 文件中包含以下内容: ``` h5Service.url=http://example.com ``` ### 4. **使用 `Environment` 验证占位符的值** 可以通过注入 `Environment` 对象来验证占位符是否被正确解析: ```java @Autowired private Environment env; @PostConstruct public void checkPlaceholder() { String h5ServiceUrl = env.getProperty("h5Service.url"); System.out.println("h5Service.url: " + h5ServiceUrl); } ``` 如果 `h5Service.url` 输出为 `null`,则说明占位符未被正确加载。 ### 5. **检查是否启用了占位符解析** 在Spring配置中,如果使用了 `@Value` 注解注入属性值,需要确保占位符解析器已启用。通常情况下,Spring Boot默认已经启用了占位符解析,但如果使用了自定义的 `PropertySourcesPlaceholderConfigurer`,则需要显式配置它: ```java @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } ``` ### 6. **检查Spring Profile是否匹配** 如果 `h5Service.url` 定义在特定的Spring Profile配置文件中(例如 `application-dev.yml`),需要确保当前激活的Profile与配置文件匹配。可以通过以下方式设置激活的Profile: - 在 `application.yml` 中设置: ```yaml spring: profiles: active: dev ``` - 或者在启动应用时通过命令行参数指定: ```bash java -jar your-application.jar --spring.profiles.active=dev ``` ### 7. **检查是否存在多模块项目中的配置隔离问题** 如果项目是一个多模块项目,确保 `h5Service.url` 被定义在主应用模块的配置文件中,而不是子模块的配置文件中。Spring Boot默认只会加载主模块的配置文件。 ### 8. **检查是否使用了错误的占位符语法** 在某些情况下,可能会使用错误的占位符语法导致解析失败。例如,在 `@Value` 注解中,应使用 `${}` 语法: ```java @Value("${h5Service.url}") private String h5ServiceUrl; ``` 如果使用了其他语法(例如 `#{}`),可能会导致占位符无法解析。 ### 9. **检查是否启用了Spring Boot的自动配置** 如果项目中禁用了Spring Boot的自动配置,可能会导致某些默认行为失效,包括占位符解析。确保没有错误地禁用自动配置: ```java @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` ###
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值