简体   繁体   English

暴露 <property-placeholder> Spring环境的属性

[英]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 of PropertyPlaceholderConfigurer . 在Spring 3.1之前, context:property-placeholder namespace元素注册了PropertyPlaceholderConfigurer一个实例。 It will still do so if using the spring-context-3.0.xsd definition of the namespace. 如果使用命名空间的spring-context-3.0.xsd定义,它仍然会这样做。 That is, you can preserve registration of PropertyPlaceholderConfigurer through the namespace, even if using Spring 3.1; 也就是说,即使使用Spring 3.1,也可以通过命名空间保留PropertyPlaceholderConfigurer注册; simply do not update your xsi: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.

相关问题 春季:访问属性设置为<context:property-placeholder> - spring: access properties set from <context:property-placeholder> Spring - “context:property-placeholder” - 属性优先级 - Spring - “context:property-placeholder” - property priority java spring context:property-placeholder-设置占位符的属性路径 - java spring context:property-placeholder - set properties paths from placeholder Spring配置文件和属性占位符异常 - Spring profile and property-placeholder exception Spring上下文:布尔值的属性占位符 - Spring context:property-placeholder for a boolean value Spring上下文:找不到属性占位符文件 - Spring context:property-placeholder file not found Spring 3.2.2属性占位符不起作用 - Spring 3.2.2 property-placeholder not working 如何使用附加到Spring中当前应用程序上下文的环境初始化property-placeholder? - How to initialize property-placeholder with environment attached to the current application context in Spring? 春天- <context:property-placeholder> 加载多个属性,但忽略缺失的属性 - spring - <context:property-placeholder> load multiple properties but ignore missing ones Spring-使用property-placeholder加载war文件或类路径之外但在文件系统中的属性文件 - Spring - Using property-placeholder to load properties file that is outside the war file or the classpath but is in the filesystem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM