java 来自另一个属性的属性占位符位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311360/
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
property-placeholder location from another property
提问by Paul McKenzie
I need to load some properties into a Spring context from a location that I don't know until the program runs.
我需要从程序运行之前我不知道的位置将一些属性加载到 Spring 上下文中。
So I thought that if I had a PropertyPlaceholderConfigurer with no locations it would read in my.locationfrom the system properties and then I could use that location in a context:property-placeholder
所以我想,如果我有一个没有位置的 PropertyPlaceholderConfigurer,它会my.location从系统属性中读取,然后我可以在上下文中使用该位置:property-placeholder
Like this
像这样
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
<context:property-placeholder location="${my.location}"/>
but this doesn't work and nor does location="classpath:${my.location}"
但这不起作用,也不行 location="classpath:${my.location}"
Paul
保罗
采纳答案by skaffman
The problem here is that you're trying to configure a property place holder using property placeholder syntax :) It's a bit of a chicken-and-egg situation - spring can't resolve your ${my.location}placeholder until it's configured the property-placeholder.
这里的问题是您正在尝试使用属性占位符语法配置属性占位符 :) 这有点像鸡和蛋的情况${my.location}——spring 在配置属性占位符之前无法解析您的占位符。
This isn't satisfactory, but you could bodge it by using more explicit syntax:
这并不令人满意,但您可以使用更明确的语法来解决它:
<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
<property name="location">
<bean class="java.lang.System" factory-method="getenv">
<constructor-arg value="my.location"/>
</bean>
</property>
</bean>
回答by Pablojim
You can do this with a slightly different approach. Here is how we configure it. I load default properties and then overrided them with properties from a configurable location. This works very well for me.
您可以使用稍微不同的方法来做到这一点。这是我们如何配置它。我加载默认属性,然后使用可配置位置的属性覆盖它们。这对我来说非常有效。
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations">
<list>
<value>classpath:site/properties/default/placeholder.properties
</value>
<value>classpath:site/properties/${env.name}/placeholder.properties
</value>
</list>
</property>
</bean>

