0

My Application is a Spring Boot Application with embedded tomcat. It uses a property file named "config.properties" for storing various application level properties. I am loading property file in my application as :

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

Application works fine when the property file is embedded in the jar file, but I want to externalize the property file - provide it from a folder in the system not from the jar.

I tried adding the folder to the classpath and then supplying the location of the folder using -cp vm argument but that does not work.

So my question is how to achieve this scenario where property file is supplied from external source rather than supplied from within the jar.

Rachit Agrawal
  • 685
  • 4
  • 13
  • 35
  • 1
    If it is spring boot just use `application.properties` and you get that behavior [out-of-the-box](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html). Work with the framework not around it. Basically remove your placeholder configurer, move properties to `application.properties`. Restart. – M. Deinum Sep 23 '15 at 07:05

2 Answers2

2

I have been able load file using the following code :

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

and starting the jar by using -

java -jar -Dconfig.file.location=D:\folder\ myjar.jar
Rachit Agrawal
  • 685
  • 4
  • 13
  • 35
0

Use this code:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>file:/full/path/to/your/file</value>
    </property>
</bean>

Using "file" you canspecify a full pathe where your config file is located.

EDIT: If you whant to use in-line arguments, remove the bean PropertyPlaceholderConfigurer and use this instead:

-Dspring.config.location=file:/path/to/file/config.properties
David Herrero
  • 704
  • 5
  • 17
  • Problem with this approach is that I cannot change location dynamically, It becomes location specific and machine specific. – Rachit Agrawal Sep 23 '15 at 07:20
  • Yes . i know, but ussually you have an specific folder in all machines that contains config files. If you dont whant to hardcode the file path, use a JVM argument: `-Dspring.config.location=file:/path/to/file` – David Herrero Sep 23 '15 at 07:28
  • If I use this approach, will i need to change anything in the PropertyPlaceHolderConfigurer declaration ? Could you point me to some example? – Rachit Agrawal Sep 23 '15 at 07:32
  • @RachitAgrawal tell us if this helped you. – David Herrero Sep 23 '15 at 08:05