[英]How to use multiple property-placeholder in a Spring xml file
我有一個與Spring如何處理多個property-placeholder相關的問題。
我有這部分代碼:
<context:property-placeholder location="classpath:dir1/${myapp.system.property}.properties"/>
屬性myapp.system.property是已定義的System屬性。
例如,如果將其定義為“devsystem”,則會導入devsystem.properties中定義的所有屬性,並且可以在下面的代碼中使用。
現在我想要另一個屬性文件,其名稱由devsystem.property文件中的屬性定義:
<context:property-placeholder location="classpath:dir1/${myapp.system.property}.properties"/>
<context:property-placeholder location="classpath:dir2/myapp-${myapp.environment}.properties"/>
myapp.environment是devsystem.properties文件中定義的屬性。
這停止了工作。 Spring無法解析$ {myapp.environment}並抱怨它無法找到文件dir2 / myapp - $ {myapp.environment} .properties。
有人能讓我知道我做錯了什么,我怎么能讓這個工作?
非常感謝你。
你可以做這樣的事情
<context:property-placeholder location="classpath:file1.properties,classpath*:project-common.properties,classpath*:project-${spring.profiles.active}.properties"/>
在我的情況下,它是一個遺留系統,所以屬性文件沒有一些標准名稱,但肯定你可以使用通配符來引用你的屬性文件。
<context:property-placeholder location="classpath:*.properties"/>
您可以使用
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:file1.properties</value>
<value>classpath:file2.properties</value>
</property>
</bean>
或(逗號分隔)
<context:property-placeholder location="classpath:file1.properties,classpath:file2.properties"/>
僅僅4年,但我找到了一種方法在春季4(我使用4.2.2)。
訣竅是使用util:properties來包裝2個屬性文件,並使用SpEL訪問第一個文件中包含的值:
<util:properties
id="specific"
location="classpath:dir1/#{systemProperties['myapp.system.property']}.properties"/>
<util:properties
id="devProperties"
location="classpath:dir2/myapp-#{specific['myapp.environment']}.properties"/>
<context:property-placeholder properties-ref="specific" order="1" ignore-unresolvable="true" />
<context:property-placeholder properties-ref="devProperties" order="2" ignore-unresolvable="true" />
使用您可以訪問bean的屬性。
ignore-unresolvable是在沒有錯誤的情況下訪問兩個屬性占位符的方法
希望從現在起幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.