0

My application is running on Jboss 4.2.2 GA with Spring 2.5.6, Richfaces 3.1.6.SR1, JSF 1.1_02. What I want is to have a porpertie file outside my ear which will contain s.th. like

  • information1="Joey"
  • information2="DeeDee"
  • information3="Marky"
  • [...]

Changes on this file should have immediate impact. I started like this:

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();
    }
}

This seems only to work if myProperties.properties is somewhere in target directory inside my ear. So I changed my code:

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);

This works fine. But I want to avoid using an absolute path.I´ve allready defined a

<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>
AlfonsSocken
  • 68
  • 2
  • 10

1 Answers1

0

You can can add some -D options to the java that starts your program. For example:

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

and then use this option in your application context like this: (Pay attention that the properties stored in file internal.properties are being overloaded by using different locations the last overload wins, so the order is important.)

    <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>

Alternatively, you can set system property (-D) from your code before you load the application context that makes use of your properties:

System.setProperty(CUSTOM_INTERNAL_PROPS_LOCATION_PROP_KEY, CUSTOM_INTERNAL_PROPS_LOCATION_PROP_VAL);
aviad
  • 8,229
  • 9
  • 50
  • 98