1

java.util.Propertiesオブジェクトをコンストラクター引数として受け取るメソッドがあります。現在、Springを通じて構築されています(ドキュメントを参照)。

<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でオーバーライド/拡張​​するにはどうすればよいですか?

4

3 に答える 3

0

あなたはこれを行うことができます:

<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>
于 2013-01-11T19:31:17.947 に答える
0

私の編集をレビューしている人はあまり役に立たず、理解していないようです。したがって、ここに完全な回答を投稿します。

<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>
于 2013-02-13T18:23:12.360 に答える
0

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>
于 2015-09-08T15:17:59.717 に答える