2012-02-10 9 views
0

私のアプリケーションは、Spring 2.5.6、Richfaces 3.1.6.SR1、JSF 1.1_02のJboss 4.2.2 GAで動作しています。 私がしたいのは、私の耳の外にporpertieファイルがあり、これにはs.thが含まれています。以下のようなSpringでのPropertiesConfigurationのファイルへの参照

  • 情報1 = "ジョーイ"
  • 情報2 = "ディーディー"
  • information3 = "マーキー"
  • は[...]このファイルの

変更は直接的な影響を持っている必要があります。 私はこのように始まっ:

import org.apache.commons.configuration.ConfigurationException; 
import org.apache.commons.configuration.PropertiesConfiguration; 
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; 

[...] 

PropertiesConfiguration configure = null; 
{ 
try { 
     configure = new PropertiesConfiguration("myProperties.properties"); 
     configure.setReloadingStrategy(new FileChangedReloadingStrategy()); 
     configure.setAutoSave(true); 

    } catch (ConfigurationException e) { 
     e.printStackTrace(); 
    } 
} 

これはmyProperties.propertiesは私の耳内部のターゲットディレクトリのどこかにある場合にのみ動作するようです。だからコードを変更しました:

File file = new File(myAppRoot + FSEP + "appserver" + FSEP + "server" + FSEP + "default" 
           + FSEP + "conf" + FSEP + "myApp" + FSEP + "myProperties.properties"); 
configure = new PropertiesConfiguration(file); 
configure.setReloadingStrategy(new FileChangedReloadingStrategy()); 
configure.setAutoSave(true); 

これはうまくいきます。しかし、私は絶対パスを使用しないようにしたいと思います。私はすでに定義済みです

<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
[...] 
<property name="locations"> 
    <list> 
    <value>classpath:myProperties.properties</value> 
    <value>classpath:myApp/myProperties.properties</value> 
    </list> 
</property> 

答えて

0

あなたのプログラムを起動するjavaに-Dオプションを追加することができます。たとえば、次のように

java ... -DjdbcPropsLocation=%YOUR_PROPERTIES_LOCATION% -DcustomInternalPropsLocation=%SOME_OTHER_PROPS_FILE_LOCATION% ... 

してから、このようなあなたのアプリケーションのコンテキストでは、このオプションを使用します(ファイルinternal.propertiesに格納されたプロパティは、異なる場所に最後の過負荷の勝利を使用することにより、オーバーロードされていることを注意してください ので、順序重要です)また

<bean id="propsLocations" class="java.util.ArrayList" > 
    <constructor-arg> 
     <list> 
      <value>classpath:jdbc.properties</value> 
      <value>${jdbcPropsLocation}\jdbc.properties</value> 
      <!-- loading the default internal properties --> 
      <value>classpath:internal.properties</value> 
      <!-- loading the meta-data.properties --> 
      <value>classpath:meta-data.properties</value> 
      <!-- overloading the internal properties with custom values --> 
      <value>${customInternalPropsLocation}\internal.properties</value> 
     </list> 
    </constructor-arg> 
</bean> 


<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations" > 
     <list> 
      <!-- <value>classpath:${jdbcPropsLocation}/jdbc.properties</value> --> 
      <value>classpath:jdbc.properties</value> 
      <value>${jdbcPropsLocation}\jdbc.properties</value> 
      <!-- loading the default internal properties --> 
      <value>classpath:internal.properties</value> 
      <!-- loading the meta-data.properties --> 
      <value>classpath:meta-data.properties</value> 
      <!-- overloading the internal properties with custom values --> 
      <value>${customInternalPropsLocation}\internal.properties</value> 
     </list> 
    </property> 
</bean> 

、あなたがあなたの特性を利用するアプリケーションのコンテキストをロードする前に、あなたのコードからシステムプロパティ(-D)を設定することができます。

System.setProperty(CUSTOM_INTERNAL_PROPS_LOCATION_PROP_KEY, CUSTOM_INTERNAL_PROPS_LOCATION_PROP_VAL);