簡體   English   中英

在Spring中合並java.util.Properties

[英]Merging java.util.Properties in Spring

我有一個將java.util.Properties對象作為構造函數參數的方法。 目前,它是通過Spring這樣構建的(請參閱docs ):

<bean id="myObj" class="myClass">
    <constructor-arg>
        <value>
            prop1=1
            prop2=2
        </value>
    </constructor-arg>
</bean>

現在,我要提取此<value>...</value>塊,以便可以創建從這些基本屬性繼承但覆蓋/刪除/添加一些屬性的其他Properties Bean。 請注意,如果可能的話,我想保留<value></value>格式。

我嘗試使用<util:properties> ,但是似乎沒有辦法使用與<value></value>相同的格式。

我也嘗試使用

<bean id="test" class="java.util.Properties">
    <constructor-args>
        <value>
             test1=1
             test2=2
        </value>
    </constructor-args>
</bean>

似乎即使java.util.Properties有一個復制構造函數,它也不起作用(給出一個空的Properties對象)。 此外,如果我有一個java.util.Properties bean,如何用另一個屬性列表/ bean覆蓋/擴展它?

你可以這樣做:

<bean name="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <value>
            test1=1
            test2=2
        </value>

    </property>
</bean>

然后參考其他地方的通用屬性集:

<bean id="myObj" class="myClass">
    <constructor-arg ref="props">
    </constructor-arg>
</bean>

似乎有人在審核我的編輯內容並沒有幫助,並且聽不懂。 因此,我將在此處發布完整答案。

<bean name="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="properties">
    <value>
        test1=1
        test2=2
    </value>

</property>
</bean>

<bean name="props2" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="propertiesArray">
  <list>
      <ref bean="props">
      <value>
           test1=OVERRIDE!
      </value>
  </list>
 </property>
</bean>

<bean id="myObj" class="myClass">
 <constructor-arg ref="props2">
 </constructor-arg>
</bean>

在Spring 3中,您可以執行以下操作:

<util:properties id="myProperties">
    <prop key="test1Key">test1Value</prop>
    <prop key="test2Key">test2Value</prop>
</util:properties>

<bean id="myObj" class="myClass">
    <constructor-arg index="0" ref="myProperties" />
</bean>

暫無
暫無

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

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