我有一个通过XML向Spring注册的属性文件,使用property-placeholder元素:
<context:property-placeholder location="classpath:foo.properties" />我可以使用@Value注释访问属性。
@Value("${prefs.key}")
private String prefValue;但是我也需要通过Spring环境访问属性。
@Autowired
private Environment env;
public String getValue(String key) {
return env.getProperty(key);
}getValue()在这里总是返回null,即使是在属性文件中定义的键,因为使用<property-placeholder>似乎不会向环境公开属性。是否有一种方法可以强制以这种方式加载的属性可以通过环境访问?
发布于 2014-01-22 08:10:46
从Spring3.2.x 参考文献和introduction 博客帖子
在Spring3.1之前,
context:property-placeholder命名空间元素注册了一个PropertyPlaceholderConfigurer实例。如果使用名称空间的spring-context-3.0.xsd定义,它仍然会这样做。也就是说,即使使用Spring3.1,也可以通过名称空间保留PropertyPlaceholderConfigurer的注册;只需不更新xsi:schemaLocation并继续使用3.0XSD。
因此,我猜您的XML没有使用正确的XSD版本。
https://stackoverflow.com/questions/21277188
复制相似问题