簡體   English   中英

如何從XML配置文件中設置Spring屬性?

[英]How can I set Spring property from the XML configuration file?

我有一些使用屬性的spring配置,如下所示:

<bean id="foo" class="...">
    <constructor-arg value="${aProperty}"/>
</bean>

顯然我知道我可以通過一個屬性文件(例如example.properties)來解析這個屬性:

aProperty=value

並在Spring配置中導入此文件:

<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>example.properties</value>
        </list>
    </property>
</bean>

我的問題是,我可以直接在XML文件中設置此屬性,而不必創建單獨的屬性文件嗎? 這樣的事情是理想的:

<set-property name="aProperty" value="value"/>

Maven對pom文件有類似的功能:

<properties><aProperty>value</aProperty></properies>

使用屬性文件的目標是從Spring配置文件中取消值,因此在同一配置文件中定義屬性有點奇怪。 不過,您始終可以向PropertyPlaceholderConfigurer添加屬性:

<bean id="propertyConfiguration" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>example.properties</value>
        </list>
    </property>
    <property name="properties">
        <props>
            <prop key="aa">bb</prop>
            <prop key="cc">dd</prop>
        </props>
    </property>
</bean>

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM