Throw exception if import locations are not found
Update config data processing code so that import locations are mandatory by default. Any import request will now throw a `ConfigDataLocationNotFoundException` if the specified import location cannot be found. For optional imports, the user can use the `optional:` prefix to indicate that the application should continue to start, even if the location does not exist. Closes gh-23032
This commit is contained in:
@@ -570,9 +570,11 @@ The following example shows how to specify two locations:
|
||||
|
||||
[indent=0]
|
||||
----
|
||||
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
|
||||
$ java -jar myproject.jar --spring.config.location=optional:classpath:/default.properties,optional:classpath:/override.properties
|
||||
----
|
||||
|
||||
TIP: Use the prefix `optional:` if the <<boot-features-external-config-optional-prefix,locations are optional>> and you don't mind if they don't exist.
|
||||
|
||||
WARNING: `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).
|
||||
|
||||
@@ -584,22 +586,22 @@ Typical extensions that are supported out-of-the-box are `.properties`, `.yaml`,
|
||||
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 `classpath:/custom-config/,file:./custom-config/`, the complete set of locations considered is:
|
||||
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:
|
||||
|
||||
. `classpath:custom-config/`
|
||||
. `file:./custom-config/`
|
||||
. `optional:classpath:custom-config/`
|
||||
. `optional:file:./custom-config/`
|
||||
|
||||
If you prefer to add addition 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 `classpath:/custom-config/,file:./custom-config/`, the complete the complete set of locations considered is:
|
||||
For example, if `spring.config.additional-location` is configured with the value `optional:classpath:/custom-config/,optional:file:./custom-config/`, the complete the complete set of locations considered is:
|
||||
|
||||
. `classpath:/`
|
||||
. `classpath:/config/`
|
||||
. `file:./`
|
||||
. `file:./config/*/`
|
||||
. `file:./config/`
|
||||
. `classpath:custom-config/`
|
||||
. `file:./custom-config/`
|
||||
. `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.
|
||||
@@ -612,6 +614,17 @@ NOTE: If your application runs in a servlet container or application server, the
|
||||
|
||||
|
||||
|
||||
[[boot-features-external-config-optional-prefix]]
|
||||
==== Optional Locations
|
||||
By default, when a specified config data location does not exist, Spring Boot will throw a `ConfigDataLocationNotFoundException` and your application will not start.
|
||||
|
||||
If you want to specify a location, but you don't mind if it doesn't always exist, you can use the `optional:` prefix.
|
||||
You can use this prefix with the `spring.config.location` and `spring.config.additional-location` properties, as well as with <<boot-features-external-config-files-importing, `spring.config.import`>> declarations.
|
||||
|
||||
For example, a `spring.config.import` value of `optional:file:./myconfig.properties` allows your application to start, even if the `myconfig.properties` file is missing.
|
||||
|
||||
|
||||
|
||||
[[boot-features-external-config-files-wildcards]]
|
||||
==== Wildcard Locations
|
||||
If a config file location includes the `{asterisk}` character for the last path segment, it is considered a wildcard location.
|
||||
@@ -662,7 +675,7 @@ For example, you might have the following in your classpath `application.propert
|
||||
[source,properties,indent=0]
|
||||
----
|
||||
spring.application.name=myapp
|
||||
spring.config.import=file:./dev.properties
|
||||
spring.config.import=optional:file:./dev.properties
|
||||
----
|
||||
|
||||
This will trigger the import of a `dev.properties` file in current directory (if such a file exists).
|
||||
@@ -718,7 +731,7 @@ To import these properties, you can add the following to your `application.prope
|
||||
|
||||
[source,properties,indent=0]
|
||||
----
|
||||
spring.config.import=configtree:/etc/config
|
||||
spring.config.import=optional:configtree:/etc/config
|
||||
----
|
||||
|
||||
You can then access or inject `myapp.username` and `myapp.password` properties from the `Environment` in the usual way.
|
||||
|
||||
Reference in New Issue
Block a user