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

猜你喜欢

转载自blog.csdn.net/xiebinyuxyz/article/details/79985053
今日推荐