Add additional grouping of property sources by profile

Before this change the PropertySources loaded from external config
files were just added to the list for resolution in the order that
they were loaded. That worked for simple cases, but when there are
profiles active, and files themselves can activate profiles, it led
to users not being able to change default settings easily (either
on command line or in files, mostly in files).

The solution proposed here is to group PropertySources by profile
and resolve them in order of profile first, and then in order of
the files being loaded.

There are additional shenanigans because the order of the files
being loaded also has to be carefully defined. The rule for users
is that in a list of files to load (e.g. if set via
spring.config.location), the last one wins (natural if you think of
it as a merge of multiple maps). In addition, anything specified
by a user takes precedence over the defaults (which was broken in
some scenarios before).

Additionally, fixes profile ordering in @ConfigurationProperties(path=...)

Fixes gh-483
This commit is contained in:
Dave Syer
2014-03-21 12:16:59 +00:00
parent 84cc110344
commit 48636e3d6e
7 changed files with 293 additions and 71 deletions

View File

@@ -249,10 +249,10 @@ them using `SpringApplication.setAddCommandLineProperties(false)`.
`SpringApplication` will load properties from `application.properties` files in the
following locations and add them to the Spring `Environment`:
. The current directory
. A `/config` subdir of the current directory.
. The classpath root
. The current directory
. A classpath `/config` package
. The classpath root
The list is ordered by precedence (locations higher in the list override lower items).
@@ -261,13 +261,29 @@ an alternative to '.properties'.
If you don't like `application.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.
to an explicit location using the `spring.config.location` environment property (comma-
separated list of directory locations, or file paths).
[indent=0]
----
$ java -jar myproject.jar --spring.config.name=myproject
----
or
[indent=0]
----
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
----
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). The default search
path `classpath:,classpath:/config,file:,file:config/` is always used,
irrespective of the value of `spring.config.location`. In that way you
can set up default values for your app in `application.properties` (or
whatever other basename you choose with `spring.config.name`) and
override it at runtime with a different file, keeping the defaults.
[[boot-features-external-config-profile-specific-properties]]