Ensure @ActiveProfiles replaces existing profiles

Update `SpringBootContextLoader` to both add `spring.profiles.active`
properties and to directly call `Environment.setActiveProfiles`.
The additional `setActiveProfiles` call prevents `AbstractEnvironment`
from accidentally loading `spring.profiles.active` properties directly
when `doGetActiveProfiles` is called.

Directly setting active profiles has only become necessary since we
started adding properties using the square bracket notation. Previously
we added a comma-separated list which would be picked up by both the
`AbstractEnvironment` and the `ConfigurationFileApplicationListener`.

Closes gh-21302
This commit is contained in:
Phillip Webb
2020-05-08 17:16:06 -07:00
parent 28749e7fbb
commit 49921d65ac
3 changed files with 155 additions and 0 deletions

View File

@@ -152,6 +152,8 @@ public class SpringBootContextLoader extends AbstractContextLoader {
}
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
environment.setActiveProfiles(profiles);
// Also add as properties to override any application.properties
String[] pairs = new String[profiles.length];
for (int i = 0; i < profiles.length; i++) {
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];