[英]Expose <property-placeholder> properties to the Spring Environment
I have a properties file that I'm registering with Spring through XML, using the property-placeholder
element: 我有一个属性文件,我通过XML注册Spring,使用property-placeholder
元素:
<context:property-placeholder location="classpath:foo.properties" />
I can access properties using @Value
annotations, eg 我可以使用@Value
注释访问属性,例如
@Value("${prefs.key}")
private String prefValue;
but I also need to access properties via the Spring Environment, eg 但我还需要通过Spring环境访问属性,例如
@Autowired
private Environment env;
public String getValue(String key) {
return env.getProperty(key);
}
getValue()
here always returns null
, even for keys defined in the properties file, because it seems that using <property-placeholder>
does not expose properties to the Environment. getValue()
在这里总是返回null
,即使对于属性文件中定义的键也是如此,因为使用<property-placeholder>
似乎不会向Environment公开属性。 Is there a way to force properties loaded this way to be accessible via the Environment? 有没有办法强制通过环境访问以这种方式加载的属性?
From Spring 3.2.x reference and introduction blog post : 从Spring 3.2.x 参考和介绍博客文章 :
Prior to Spring 3.1, the
context:property-placeholder
namespace element registered an instance ofPropertyPlaceholderConfigurer
. 在Spring 3.1之前,context:property-placeholder
namespace元素注册了PropertyPlaceholderConfigurer
一个实例。 It will still do so if using thespring-context-3.0.xsd
definition of the namespace. 如果使用命名空间的spring-context-3.0.xsd
定义,它仍然会这样做。 That is, you can preserve registration ofPropertyPlaceholderConfigurer
through the namespace, even if using Spring 3.1; 也就是说,即使使用Spring 3.1,也可以通过命名空间保留PropertyPlaceholderConfigurer
注册; simply do not update yourxsi:schemaLocation
and continue using the 3.0 XSD. 只是不要更新您的xsi:schemaLocation
并继续使用3.0 XSD。
So, my guess is that your XMLs are not using the proper XSD version. 所以,我的猜测是你的XML 没有使用正确的XSD版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.