java PropertyPlaceholderConfigurer + PropertiesFactoryBean 仅解析位置属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10414419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PropertyPlaceholderConfigurer + PropertiesFactoryBean only resolving location properties
提问by guydog28
I am having trouble getting PropertyPlaceholderConfigurer to work in my current configuration. Given the following block of code in my applicationContext.xml:
我无法让 PropertyPlaceholderConfigurer 在我当前的配置中工作。鉴于我的 applicationContext.xml 中的以下代码块:
<bean id="myProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<!-- Order matters, last one to create a property wins! -->
<value>classpath:default.properties</value>
<value>file:${MYAPP_PROPERTIES_LOCATION:badurl}/application.properties</value>
<value>file:${user.home}/developer.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertiesPlaceholderConfigurer">
<property name="properties" ref="myProperties"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="searchSystemEnvironment" value="true"/>
</bean>
Then in the default.properties files I have the following (these are test properties):
然后在 default.properties 文件中,我有以下内容(这些是测试属性):
property1=prop1val
property2=${property1}
What works: the propertyConfigurer correctly resolves both the environment variable MYAPP_PROPERTIES_LOCATION and the System variable user.home. However, then the final properties object is created by the factory bean, the resulting properties are [property1=prop1val, property2=${property1}].
什么有效:propertyConfigurer 正确解析环境变量 MYAPP_PROPERTIES_LOCATION 和系统变量 user.home。然而,最终的属性对象是由工厂 bean 创建的,结果属性是 [property1=prop1val, property2=${property1}]。
With this configuration, how can I get the properties inside the myProperties bean to resolve their placeholders??I have done plenty of research to include tracing through the spring code - and I can see how and why this isn't being done. I'm hoping there is some setting I'm just missing! This is my first post so be easy on me :)
使用此配置,如何获取 myProperties bean 中的属性以解析它们的占位符?我已经做了大量的研究来包括对 spring 代码的跟踪 - 我可以看到如何以及为什么没有这样做。我希望有一些我只是缺少的设置!这是我的第一篇文章,所以请对我放轻松:)
采纳答案by ManuPK
You have proper configuration of spring and the property files. That why you are being able to read the data from the file. Everything is proper with what spring is doing too, let me explain a bit more....
您已正确配置 spring 和属性文件。这就是为什么您能够从文件中读取数据的原因。春天所做的一切都很好,让我再解释一下......
As you told below is your property file,
正如您在下面所说的是您的财产文件,
property1=prop1val
property2=${property1}
Remember, it is a pretty text file with key value pairs and it can not take variables. Here, if you are intended to copy the value of property1
to property2
dynamically it is not going to happen. That is not the way we are supposed to use a property file.
请记住,它是一个带有键值对的漂亮文本文件,它不能接受变量。在这里,如果你是打算的值复制property1
到property2
动态这是不会发生的。这不是我们应该使用属性文件的方式。
Property file is supposed to be simple text file with key-value pairs. So, keep the keys atomic so that you can construct the required data logically from the application or inside your applicationcontext.xml
file.
属性文件应该是带有键值对的简单文本文件。因此,保持键的原子性,以便您可以从应用程序或applicationcontext.xml
文件中逻辑地构建所需的数据。
回答by Santosh
I am not sure why you are using PropertiesFactoryBean
. Can you please try just the following (Not sure if you already tried it and any issues you faced)
我不确定你为什么使用PropertiesFactoryBean
. 您能否仅尝试以下操作(不确定您是否已经尝试过以及您遇到的任何问题)
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- Order matters, last one to create a property wins! -->
<value>classpath:default.properties</value>
<value>file:${MYAPP_PROPERTIES_LOCATION:badurl}/application.properties</value>
<value>file:${user.home}/developer.properties</value>
</list>
</property>
</bean>