Could not resolve placeholder原因分析及解决方案

1. 问题描述

   在启动Junit跑单测加载资源配置文件的时候遇到以下异常信息:

 

Java代码 
  1. Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'activity_template_id' in string value "${activity_template_id}"  
  2.     at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)  
  3.     at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)  
  4.     at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258)  
  5.     at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)  
  6.     at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)  
  7.     at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)  
  8.     at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)  
  9.     at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:206)  
  10.     ... 29 more  

 

2. 问题分析

在读取配置问题信息的时候使用了入下方法:

 

Java代码 
  1. "propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
  2.           depends-on="genernatePropertyFile">  
  3.         "location">  
  4.             file:D:/idev/antx.properties  
  5.           
  6.   
 

那么出现异常信息的可能性有三种

(1)location中的属性文件配置错误

(2)location中定义的配置文件里面没有对应的placeholder值

(3)第三种就比较麻烦点,可能是Spring容器的配置问题

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。 

而这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。 

 

3.解决方案

(1)

Java代码 
  1. "propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"  
  2.           depends-on="genernatePropertyFile">  
  3.         "location">  
  4.             file:D:/idev/antx.properties  
  5.           
  6.         "ignoreUnresolvablePlaceholders" value="true  
  7.   
 

(2)

Java代码 
  1. "classpath*:redis.properties" ignore-unresolvable="true" />  
       但是 ignore-unresolvable="true" 和 这两个属性值必须为true
### Java程序中无法解析占位符 `redis.host1` 的原因分析 在Spring框架的应用场景下,如果遇到错误提示 `java.lang.IllegalArgumentException: Could not resolve placeholder 'redis.host1'`,这通常表明Spring容器未能成功解析指定的属性值。以下是可能的原因及其解决方案: #### 原因一:未正确加载配置文件 如果配置文件路径设置不正确或者使用了通配符而实际并未匹配到任何有效的`.properties`文件,则可能导致目标属性未被加载。 ```xml <bean id="propertyConfig" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <!-- 如果此处路径有误或无匹配项 --> <value>classpath*:config/*.properties</value> </list> </property> </bean> ``` 此情况下的解决方法可以参照以下建议[^2]: - 明确指定具体的配置文件路径而非依赖于模糊匹配。 - 验证项目资源目录下是否存在预期的`.properties`文件并确认其名称拼写准确无误。 #### 原因二:重复定义 PropertyPlaceholderConfigurer 导致冲突 当同一个Spring应用上下文中存在多个`PropertyPlaceholderConfigurer` Bean时,只有第一个会被激活用于处理占位符替换操作,其余均失效。因此如果你尝试访问由后续声明者管理的属性就可能出现异常。 例如下面这段XML片段展示了如何创建一个标准的`PropertyPlaceholderConfigurer` bean来读取外部化的数据库连接参数: ```xml <context:property-placeholder location="classpath:db.properties"/> <!-- 若再额外增加如下行则会造成竞争状况--> <!--<context:property-placeholder location="classpath:cache.properties"/>--> ``` 针对此类情形可采取措施之一便是合并所有必要的键值对至单一源文件内[^1];另一种方式则是调整各自作用域以避免相互干扰。 #### 命名约定失误引发查找失败 另外还需注意检查变量标识符本身是否遵循既定模式以及大小写敏感度等问题。比如案例里提到由于笔误把原本应该指向production环境设定档的名字搞混成了development版本形式等等[^4]。 最后附带一段示范代码用来验证最终生效后的全局级联覆盖逻辑效果演示: ```java @Value("${redis.host}") private String redisHost; public void testRedisConnection() { System.out.println("Connecting to Redis at host:" + redisHost); } ``` 以上就是关于该类问题的一些常见排查方向总结分享给大家作为参考依据之用!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值