我在Java代码中对我的属性有以下定义:
import org.springframework.beans.factory.annotation.Value;
...
@Value("#{sdProperties['is.test.server'] ?: false }")
private boolean isTestServer = false;我还在XML配置文件中拥有:
<util:properties id="sdProperties">
<prop key="sdzootest.server.url">${sdzootest.server.url}</prop>
<prop key="is.test.server">${is.test.server}</prop>
</util:properties> 不过,如果属性文件中未指定is.test.server,则仍会收到错误:
2016-04-06 15:52:00,161本地主机-startStop-1错误com.elasticpath.web.context.impl.EpContextConfigListener:69 -捕获了一个异常。org.springframework.beans.factory.BeanDefinitionStoreException:无效bean定义,其名称sdProperties定义为null:无法解析字符串值"${is.test.server}“中的占位符'is.test.server‘
发布于 2016-04-06 21:30:28
PlaceholderConfigurerSupport有一个特殊的属性ignoreUnresolvablePlaceholders
如果配置程序无法解析占位符,则将引发BeanDefinitionStoreException。如果要针对多个属性文件进行检查,请通过“位置”属性指定多个资源。您还可以定义多个配置器,每个配置器都有自己的占位符语法。如果占位符无法解析,则使用ignoreUnresolvablePlaceholders故意阻止抛出异常。
尚不清楚如何设置占位符支持,因此这里有几个选项:
<context:property-placeholder
ignore-unresolvable="true"
location="classpath:app.properties"/>或
@Bean
public PropertySourcesPlaceholderConfigurer ppc() {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc;
}https://stackoverflow.com/questions/36461922
复制相似问题