Skip to content

Commit 28cf6f5

Browse files
committed
Merge branch '2.5.x' into 2.6.x
Closes gh-29410
2 parents 14fe934 + 79d9549 commit 28cf6f5

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -87,14 +87,15 @@ public static void attach(Environment environment) {
8787
Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
8888
MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
8989
PropertySource<?> attached = getAttached(sources);
90-
if (attached != null && attached.getSource() != sources) {
90+
if (attached != null) {
91+
if (attached instanceof ConfigurationPropertySourcesPropertySource
92+
&& ((SpringConfigurationPropertySources) attached.getSource()).isUsingSources(sources)) {
93+
return;
94+
}
9195
sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
92-
attached = null;
93-
}
94-
if (attached == null) {
95-
sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
96-
new SpringConfigurationPropertySources(sources)));
9796
}
97+
sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME,
98+
new SpringConfigurationPropertySources(sources)));
9899
}
99100

100101
static PropertySource<?> getAttached(MutablePropertySources sources) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,6 +71,19 @@ void attachShouldReattachInMergedSetup() {
7171
assertThat(child.getProperty("my.example-property")).isEqualTo("1234");
7272
}
7373

74+
@Test
75+
void attachWhenAlreadyAttachedWithSameSourcesShouldReturnExistingInstance() {
76+
ConfigurableEnvironment environment = new StandardEnvironment();
77+
MutablePropertySources sources = environment.getPropertySources();
78+
sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234")));
79+
sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568")));
80+
ConfigurationPropertySources.attach(environment);
81+
Iterable<ConfigurationPropertySource> first = ConfigurationPropertySources.get(environment);
82+
ConfigurationPropertySources.attach(environment);
83+
Iterable<ConfigurationPropertySource> second = ConfigurationPropertySources.get(environment);
84+
assertThat(first).isSameAs(second);
85+
}
86+
7487
@Test
7588
void getWhenNotAttachedShouldReturnAdapted() {
7689
ConfigurableEnvironment environment = new StandardEnvironment();

0 commit comments

Comments
 (0)