16

I am trying to load an external properties file into my spring boot app. initially I used @PropertySource in the config class. but now I want to remove this annotation so the class is not dependent on the location. so I tried to use:

java -jar my-boot-ws.war --SPRING_CONFIG_NAME=file:///Users/TMP/resources/ 

based on this http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html documentation but I get the following error:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 

using the annotation works fine but I would really like to move away from that. any help on this would be great

Thanks

****** CORRECTION *******

Sorry copy paste error the above command was supposed to be:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/ 

I'm not trying to change the name of the config file just add an additional location. As explained here:

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded).

I interpreted this as saying that the file ${spring.application.name}.properties would be loaded from the --spring.config.location passed in from the command line

5 Answers 5

29

After some more googeling I found this Spring Boot and multiple external configuration files indicating that the following is the correct usage:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties 

I was under the impression that the --spring.config.location would load other properties files in the directory specified. according to the post at the link I mentioned this is not the case. based on the link if the directory is specified then that is where the application.properties is searched for. but again the documentation here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html seems to insinuate that the spring boot app will look on the class path first and if available grab the app name to get additional properties files based on that name.

however once I specified a file name everything worked fine so I guess I was mistaken.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I'm faced with same situation, where various property files are located under folder & subfolder. I do not know how to get/load all those properties file from those folders. How can I mention only the folders paths so that all the property files get loaded.
this didn't work for some reason in ubuntu however the other answer did where the config was passed like --spring.config.location="file:/path/to/application.properties"
19

In command line you should use below property to mention an additional boot configuration file:

--spring.config.location="file:/path/to/application.properties" 

An alternative would be:

-Dspring.config.location="file:/path/to/application.properties" 

Note that characters are lower case and the word separator is a period ..

Otherwise you can use an environment variable with key you used already:

  • In a *nix system:

    export SPRING_CONFIG_NAME=file:/path/to/application.properties 
  • In Windows OS:

    set SPRING_CONFIG_NAME=file:/path/to/application.properties 

9 Comments

I corrected my statement above, the name is not what I wanted to change. I wanted to add a location.
You question was missleading since it used a wrong system property name. Recheck my answer and if it looks correct to you, please accept or upvote it :)
your answer is technically correct and worthy of an up vote but does not answer the intended question. I do apologize for the copy/paste error being misleading.
I respect your position, but you may better improve your response with detailed steps along with the provided description :)
I assumed that since the detailed steps were provided in the link there was no need to duplicate it.
|
4

It might not be a common issue, but I faced it. You also must have an application.properties inside your classpath even when you replace it with --spring.config.name (I had mine in gitignore due to sensitive information).

2 Comments

I am trying to achieve what you did. what's your external file name and can you give me the command line that you use? I tried ./mvnw -Dmaven.test.skip=true -Dspring.config.additional-location=file:./secret.yml -Dspring.config.name=secretbut this doesn't work
Hi Hamdy, the way that worked for me was to override in the execution time, for example, my command was: java -jar (JVM OPTS) myLib.jar --spring.config.location=application.properties (In my case both files has the same name, the one in the classpath and the other in the filesystem). For maven we just have the main file in src/main/resources and for tests in src/test/resources, this way we can override the content for tests. I hope it helped you.
2

1) Makesure args pass inside of run method

public class GemFireTestLoaderApplication { public static void main(String[] args) { SpringApplication.run(GemFireTestLoaderApplication.class, args); } } 

2) If you have configureed in xml comment or remove first

<!-- <context:property-placeholder location="classpath:config.properties" /> --> <!-- <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> --> 

Following command you can use to pass properties name

3.1)

java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties 

3.2)

java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties 

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

2 Comments

Your first point was what I was missing. Passing args helped! Thank you!
I had the same issue. args was not being passed. Upvoted the answer and the comment. Thanks!
-1
spring.config.name=spring spring.config.location=classpath:/config/ 

in side the config folder spring.properties file is available, while running the server the this properties file is not loading

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.