1

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.

1
  • 1
    If it is spring boot just use application.properties and you get that behavior out-of-the-box. Work with the framework not around it. Basically remove your placeholder configurer, move properties to application.properties. Restart. Commented Sep 23, 2015 at 7:05

2 Answers 2

3

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 
Sign up to request clarification or add additional context in comments.

1 Comment

ths for sharing your answer ;)
1

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 

4 Comments

Problem with this approach is that I cannot change location dynamically, It becomes location specific and machine specific.
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
If I use this approach, will i need to change anything in the PropertyPlaceHolderConfigurer declaration ? Could you point me to some example?
@RachitAgrawal tell us if this helped you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.