Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot external config

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

like image 623
peekay Avatar asked Nov 11 '14 17:11

peekay


People also ask

How do you call external properties from spring boot?

properties as the configuration file name you can switch to another by specifying a spring.config.name environment property. You can also refer to an explicit location using the spring. config. location environment property (comma-separated list of directory locations, or file paths).

What are the possible sources of external configuration?

This way, a configuration can be provided from different sources, for example, file systems, the internet, external services, configuration services, vaults, etc.


2 Answers

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.

like image 176
peekay Avatar answered Oct 13 '22 07:10

peekay


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
    
like image 21
tmarwen Avatar answered Oct 13 '22 09:10

tmarwen