You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow groups to be used with standard locations so that order of
profile-specific files is consistent.
Prior to this commit, the default search locations considered for
application properties/yaml files was the following:
optional:classpath:/
optional:classpath:/config/
optional:file:./
optional:file:./config/
optional:file:./config/*/
Each of these locations was independent which could cause confusion
if certain combinations were used. For example, if profile-specific
files were added to `classpath:/` and `classpath:/config/` then the
latter would always override the former regardless of the profile
ordering.
This commit updates `StandardConfigDataLocationResolver` so that a
group of locations can be specified for each item. This allows us to
define the following set of search locations which provide more logical
ordering for profile-specific files
optional:classpath:/;optional:classpath:/config/
optional:file:./;optional:file:./config/;optional:file:./config/*/
Closesgh-26593
Spring Boot will automatically find and load `application.properties` and `application.yaml` files from the following locations when your application starts:
608
608
609
-
. The classpath root
610
-
. The classpath `/config` package
611
-
. The current directory
612
-
. The `/config` subdirectory in the current directory
613
-
. Immediate child directories of the `/config` subdirectory
609
+
. From the classpath
610
+
.. The classpath root
611
+
.. The classpath `/config` package
612
+
. From the current directory
613
+
.. The current directory
614
+
.. The `/config` subdirectory in the current directory
615
+
.. Immediate child directories of the `/config` subdirectory
614
616
615
617
The list is ordered by precedence (with values from lower items overriding earlier ones).
616
618
Documents from the loaded files are added as `PropertySources` to the Spring `Environment`.
617
619
618
620
If you do not like `application` as the configuration file name, you can switch to another file name by specifying a configprop:spring.config.name[] environment property.
619
-
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).
620
-
The following example shows how to specify a different file name:
621
+
For example, to look for `myproject.properties` and `myproject.yaml` files you can run your application as follows:
The following example shows how to specify two locations:
628
+
You can also refer to an explicit location by using the configprop:spring.config.location[] environment property.
629
+
This properties accepts a comma-separated list of one or more locations to check.
630
+
631
+
The following example shows how to specify two distinct files:
628
632
629
633
[indent=0]
630
634
----
@@ -636,12 +640,19 @@ TIP: Use the prefix `optional:` if the <<boot-features-external-config-optional-
636
640
WARNING: `spring.config.name`, `spring.config.location`, and `spring.config.additional-location` are used very early to determine which files have to be loaded.
637
641
They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).
638
642
639
-
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).
643
+
If `spring.config.location` contains directories (as opposed to files), they should end in `/`.
644
+
At runtime they will be appended with the names generated from `spring.config.name` before being loaded.
640
645
Files specified in `spring.config.location` are used as-is.
641
-
Whether specified directly or contained in a directory, configuration files must include a file extension in their name.
642
-
Typical extensions that are supported out-of-the-box are `.properties`, `.yaml`, and `.yml`.
643
646
644
-
When multiple locations are specified, the later ones can override the values of earlier ones.
647
+
In most situations, each configprop:spring.config.location[] item you add will reference a single file or directory.
648
+
Locations are processed in the order that they are defined and later ones can override the values of earlier ones.
If you have a complex location setup, and you use profile-specific configuration files, you may need to provide further hints so that Spring Boot knows how they should be grouped.
652
+
A location group is a collection of locations that are all considered at the same level.
653
+
For example, you might want to group all classpath locations, then all external locations.
654
+
Items within a location group should be separated with `;`.
655
+
See the example in the "`<<boot-features-external-config-files-profile-specific>>`" section for more details.
645
656
646
657
Locations configured by using `spring.config.location` replace the default locations.
647
658
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:
@@ -653,11 +664,8 @@ If you prefer to add additional locations, rather than replacing them, you can u
653
664
Properties loaded from additional locations can override those in the default locations.
654
665
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:
@@ -718,6 +726,35 @@ Profile-specific properties are loaded from the same locations as standard `appl
718
726
If several profiles are specified, a last-wins strategy applies.
719
727
For example, if profiles `prod,live` are specified by the configprop:spring.profiles.active[] property, values in `application-prod.properties` can be overridden by those in `application-live.properties`.
720
728
729
+
[NOTE]
730
+
====
731
+
The last-wins strategy applies at the <<boot-features-external-config-files-location-groups,location group>> level.
732
+
A configprop:spring.config.location[] of `classpath:/cfg/,classpath:/ext/` will not have the same override rules as `classpath:/cfg/;classpath:/ext/`.
733
+
734
+
For example, continuing our `prod,live` example above, we might have the following files:
735
+
736
+
----
737
+
/cfg
738
+
application-live.properties
739
+
/ext
740
+
application-live.properties
741
+
application-prod.properties
742
+
----
743
+
744
+
When we have a configprop:spring.config.location[] of `classpath:/cfg/,classpath:/ext/` we process all `/cfg` files before all `/ext` files:
745
+
746
+
. `/cfg/application-live.properties`
747
+
. `/ext/application-prod.properties`
748
+
. `/ext/application-live.properties`
749
+
750
+
751
+
When we have `classpath:/cfg/;classpath:/ext/` instead (with a `;` delimiter) we process `/cfg` and `/ext` at the same level:
752
+
753
+
. `/ext/application-prod.properties`
754
+
. `/cfg/application-live.properties`
755
+
. `/ext/application-live.properties`
756
+
====
757
+
721
758
The `Environment` has a set of default profiles (by default, `[default]`) that are used if no active profiles are set.
722
759
In other words, if no profiles are explicitly activated, then properties from `application-default` are considered.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataLocation.java
+26Lines changed: 26 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -97,6 +97,32 @@ public Origin getOrigin() {
97
97
returnthis.origin;
98
98
}
99
99
100
+
/**
101
+
* Return an array of {@link ConfigDataLocation} elements built by splitting this
102
+
* {@link ConfigDataLocation} around a delimiter of {@code ";"}.
103
+
* @return the split locations
104
+
* @since 2.4.7
105
+
*/
106
+
publicConfigDataLocation[] split() {
107
+
returnsplit(";");
108
+
}
109
+
110
+
/**
111
+
* Return an array of {@link ConfigDataLocation} elements built by splitting this
112
+
* {@link ConfigDataLocation} around the specified delimiter.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java
+16-5Lines changed: 16 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -115,7 +115,16 @@ public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDat
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationTests.java
+33-1Lines changed: 33 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2012-2020 the original author or authors.
2
+
* Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
0 commit comments