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

peekay
  • 1,259
  • 2
  • 25
  • 49

5 Answers5

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.

Community
  • 1
  • 1
peekay
  • 1,259
  • 2
  • 25
  • 49
  • 1
    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. – Sanjeev Apr 28 '17 at 19:01
  • 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"` – RBz Jul 09 '22 at 16:36
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
    
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • I corrected my statement above, the name is not what I wanted to change. I wanted to add a location. – peekay Nov 11 '14 at 21:53
  • 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 :) – tmarwen Nov 12 '14 at 07:09
  • 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. – peekay Nov 12 '14 at 14:18
  • 2
    I respect your position, but you may better improve your response with detailed steps along with the provided description :) – tmarwen Nov 12 '14 at 15:03
  • I assumed that since the detailed steps were provided in the link there was no need to duplicate it. – peekay Nov 12 '14 at 17:39
  • Yes it does actually, it would be better to detail the steps in the anwser and mention the resource as a reference. – tmarwen Nov 13 '14 at 06:59
  • For my grails 3 app, I used -Dspring.config.location and added the following hook to build.gradle: tasks.withType(JavaExec).each { task -> task.systemProperties System.properties } – RMorrisey Sep 14 '17 at 14:29
  • You use `spring.config.name` but specified a location ... this looks incorrect to me. – aliopi Feb 05 '18 at 15:27
  • Thanks ~aliopi, I've fixed the answer. – tmarwen Feb 05 '18 at 17:54
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).

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
lgonzales
  • 101
  • 4
  • 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 – Hamdy Feb 05 '20 at 11:09
  • 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. – lgonzales Feb 07 '20 at 10:13
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

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
-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