https://www.baeldung.com/spring-properties-file-outside-jar
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html
----------------------------------------------------------------------------
By default, Spring Boot looks for "application.properties", "application.yml" inside
locations defined by "spring.config.location" Application Java Arguments
Note :
-Dfoo.bar=fubar (JVM Arg) (-D)
--foo.bar=fubar (Application Arg) (--)
If If you prefer to add additional locations, rather than replacing them, you can use "spring.config.additional-location"
Spring Boot - will look for Files in these config Locations apart from Usual Config Directories inside the Project and read them and Will be used to fill Values from @Value Annotations at Startup
@Configuration
public class SystemPropertiesFilepaths {
@Value("${applicationProperties}")
private String applicationProperties;
@Value("${processDetails}")
private String processDetails;
@Value("${databaseProperties}")
private String databaseProperties;
}
//Getters or Field Level Accessors used to fetch Values
----------------------------------------------------------------------------
export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config
----------------------------------------------------------------------------
Using the Default Location
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:
- A /config subdirectory of the current directory
Using the Command Line
We can also pass a folder location where the application will search for the file:
java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config
Using Environment Variables
Or, let's say that we can't alter the start-up command. What's great is Spring Boot will also read the environment variables SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION
export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar
2.3. External Application Properties
Spring Boot will automatically find and load application.properties
and application.yaml
files from the following locations when your application starts:
The classpath root
The classpath /config
package
The current directory
The /config
subdirectory in the current directory
Immediate child directories of the /config
subdirectory
The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as PropertySources
to the Spring Environment
.
If you do not like application
as the configuration file name, you can switch to another file name by specifying a spring.config.name
environment property. You can also refer to an explicit location by using the spring.config.location
environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:
$ java -jar myproject.jar --spring.config.name=myproject
The following example shows how to specify two locations:
$ java -jar myproject.jar --spring.config.location=optional:classpath:/default.properties,optional:classpath:/override.properties
| spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument). |
If spring.config.location
contains directories (as opposed to files), they should end in /
(at runtime they will be appended with the names generated from spring.config.name
before being loaded). Files specified in spring.config.location
are used as-is. Whether specified directly or contained in a directory, configuration files must include a file extension in their name. Typical extensions that are supported out-of-the-box are .properties
, .yaml
, and .yml
.
When multiple locations are specified, the later ones can override the values of earlier ones.
Locations configured by using spring.config.location
replace the default locations. For example, if spring.config.location
is configured with the value optional:classpath:/custom-config/,optional:file:./custom-config/
, the complete set of locations considered is:
optional:classpath:custom-config/
optional:file:./custom-config/
If you prefer to add additional locations, rather than replacing them, you can use spring.config.additional-location
. Properties loaded from additional locations can override those in the default locations. For example, if spring.config.additional-location
is configured with the value optional:classpath:/custom-config/,optional:file:./custom-config/
, the complete set of locations considered is:
optional:classpath:/
optional:classpath:/config/
optional:file:./
optional:file:./config/
optional:file:./config/*/
optional:classpath:custom-config/
optional:file:./custom-config/
This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties
(or whatever other basename you choose with spring.config.name
) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.