Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PropertyPlaceholderConfigurer: can i have a dynamic location value

Tags:

java

right now i have this in my xml file:

         <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
    <property name="username" value="root" />
    <property name="password" value="${jdbc.password}" />
</bean>

and

            <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>file:C:/jdbc.properties</value>
  </property>
</bean>

Now , my problem is that i do not know the exact location of this file(jdbc.properties) , as this application will going to run on different computers , in some places its installed in c: ,sometimes may be on f:.. So if i dont know the path of this file , if there is anyway i could find it .

thanks

like image 661
junaidp Avatar asked Apr 21 '13 01:04

junaidp


2 Answers

You can define the file location as system property, eg -Dprops.file=file:c:/1.properties

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>$props.file</value>
  </property>
</bean>

or

<context:property-placeholder location="${props.file}"/>

or you can scan the file system

public class ScanningPropertyPlaceholderConfigurer extends org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
    @Override
       public void setLocation(Resource location) {
       File file = findFile(fileName);  // implement file finder
       super.setLocation(new FileSystemResource(file));
    }
}
like image 172
Evgeniy Dorofeev Avatar answered Sep 27 '22 21:09

Evgeniy Dorofeev


PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("context.properties"));

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}, false);
applicationContext.addBeanFactoryPostProcessor(configurer);
applicationContext.refresh();
like image 32
Ru5 Avatar answered Sep 27 '22 23:09

Ru5