@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder

时间:2024-04-10 15:35:12

项目场景:

通过配置文件的属性做推送版本的控制
@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder
@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder
@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder


问题描述:

最后发现的结果是无法解析占位符

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘xiaoMiService’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘server.profiles.active’ in value “${server.profiles.active}”

@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder


原因分析:

可能是属性没有值或者没有从nacos读取,这里通过banner.txt 打印配置文件的属性
@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder

@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder
结果和猜测的结果是一样的没有值


解决方案:

xxx.properties–配置文件
spring.profiles.active – ${spring.profiles.active}

bootstrap.yml–Spring配置文件
server.profiles.active – ${server.profiles.active}

通过xxx.properties配置文件的spring.profiles.active这个值,
传递到bootstrap.yml中的server.profiles.active属性
@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder
结果发现server.profiles.active为空
@Value引入属性失败 java.lang.IllegalArgumentException: Could not resolve placeholder
把server.profiles.active替换成spring.profiles.active即可

总结就是多多查看自己的代码(有时候就是那么诡异)