简体   繁体   中英

Externalized Configuration in Spring boot

I have a external configuration file(out side jar). I try to run and expected that value in external file will override value in internal file( application.properties in \\resource\\ - in jar file). I read Documentation and try this:

java -jar ccgame-1.0.jar --spring.config.location=classpath:/application.properties,file:/production.properties

This not working.

My jar file at \\target\\ directory and my production.properties too(at \\target\\ )

How can I resolve my problem?

  • Where should I put external config file ?
  • And what I have to do ?

Starting from Spring Boot 2.0 it's possible to use property spring.config.additional-location . With this property, you can set external config file, but properties from that config will only override the corresponding ones from internal config, leaving other properties unchanged.

More about it in docs .

If you need to completely override the whole config, then continue to use spring.config.location property instead.

By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml – in 4 predetermined locations in the following order of precedence:

  1. /config subdirectory of the current directory
  2. The current directory
  3. Classpath /config package
  4. The classpath root

You can place your application.properties in any of the 4 locations without needing to give the location of application.properties while executing the jar. If you want to given any other custom location , then you will have to provide the path of the config location while executing the jar:

java -jar -Dspring.config.location=<path-to-file> myProject.jar

Source: https://www.baeldung.com/spring-properties-file-outside-jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM