Expose <property-placeholder> properties to the Spring Environment

牧云@^-^@ 提交于 2019-12-03 13:51:16

问题


I have a properties file that I'm registering with Spring through XML, using the property-placeholder element:

<context:property-placeholder location="classpath:foo.properties" />

I can access properties using @Value annotations, e.g.

@Value("${prefs.key}")
private String prefValue;

but I also need to access properties via the Spring Environment, e.g.

@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. Is there a way to force properties loaded this way to be accessible via the Environment?


回答1:


From Spring 3.2.x reference and introduction blog post:

Prior to Spring 3.1, the context:property-placeholder namespace element registered an instance of PropertyPlaceholderConfigurer. It will still do so if using the spring-context-3.0.xsd definition of the namespace. That is, you can preserve registration of PropertyPlaceholderConfigurer through the namespace, even if using Spring 3.1; simply do not update your xsi:schemaLocation and continue using the 3.0 XSD.

So, my guess is that your XMLs are not using the proper XSD version.



来源:https://stackoverflow.com/questions/21277188/expose-property-placeholder-properties-to-the-spring-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!