【问题标题】:Reference to file for PropertiesConfiguration with Spring使用 Spring 引用 PropertiesConfiguration 文件
【发布时间】:2012-02-10 08:16:10
【问题描述】:

我的应用程序在 Jboss 4.2.2 GA 上运行,带有 Spring 2.5.6、Richfaces 3.1.6.SR1、JSF 1.1_02。 我想要的是在我的耳朵外面有一个包含 s.th 的 porpertie 文件。喜欢

  • information1="乔伊"
  • information2="DeeDee"
  • information3="Marky"
  • [...]

对此文件的更改应该会立即产生影响。 我是这样开始的:

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>

【问题讨论】:

    标签: java apache spring jboss apache-commons-config


    【解决方案1】:

    您可以在启动程序的 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);
    

    【讨论】:

      猜你喜欢
      • 2011-12-31
      • 2015-10-02
      • 2012-04-13
      • 1970-01-01
      • 2014-11-17
      • 2012-02-17
      • 1970-01-01
      • 2011-04-06
      • 2014-06-18
      相关资源
      最近更新 更多