【发布时间】:2014-02-12 04:07:47
【问题描述】:
我有一个属性文件,我正在使用 property-placeholder 元素通过 XML 向 Spring 注册:
<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> 不会将属性暴露给环境。有没有办法强制以这种方式加载的属性可以通过环境访问?
【问题讨论】:
标签: java spring properties-file spring-environment