diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java index 97d0a2b65b..a93a4ef5c4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,14 +87,15 @@ public final class ConfigurationPropertySources { Assert.isInstanceOf(ConfigurableEnvironment.class, environment); MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources(); PropertySource attached = getAttached(sources); - if (attached != null && attached.getSource() != sources) { + if (attached != null) { + if (attached instanceof ConfigurationPropertySourcesPropertySource + && ((SpringConfigurationPropertySources) attached.getSource()).isUsingSources(sources)) { + return; + } sources.remove(ATTACHED_PROPERTY_SOURCE_NAME); - attached = null; - } - if (attached == null) { - sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME, - new SpringConfigurationPropertySources(sources))); } + sources.addFirst(new ConfigurationPropertySourcesPropertySource(ATTACHED_PROPERTY_SOURCE_NAME, + new SpringConfigurationPropertySources(sources))); } static PropertySource getAttached(MutablePropertySources sources) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java index b13217af1f..af0490ea98 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -71,6 +71,19 @@ class ConfigurationPropertySourcesTests { assertThat(child.getProperty("my.example-property")).isEqualTo("1234"); } + @Test + void attachWhenAlreadyAttachedWithSameSourcesShouldReturnExistingInstance() { + ConfigurableEnvironment environment = new StandardEnvironment(); + MutablePropertySources sources = environment.getPropertySources(); + sources.addLast(new SystemEnvironmentPropertySource("system", Collections.singletonMap("SERVER_PORT", "1234"))); + sources.addLast(new MapPropertySource("config", Collections.singletonMap("server.port", "4568"))); + ConfigurationPropertySources.attach(environment); + Iterable first = ConfigurationPropertySources.get(environment); + ConfigurationPropertySources.attach(environment); + Iterable second = ConfigurationPropertySources.get(environment); + assertThat(first).isSameAs(second); + } + @Test void getWhenNotAttachedShouldReturnAdapted() { ConfigurableEnvironment environment = new StandardEnvironment();