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 4e57523359..002c9e084d 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-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -73,7 +73,12 @@ public final class ConfigurationPropertySources { Assert.isInstanceOf(ConfigurableEnvironment.class, environment); MutablePropertySources sources = ((ConfigurableEnvironment) environment) .getPropertySources(); - if (!sources.contains(ATTACHED_PROPERTY_SOURCE_NAME)) { + PropertySource attached = sources.get(ATTACHED_PROPERTY_SOURCE_NAME); + if (attached != null && attached.getSource() != sources) { + sources.remove(ATTACHED_PROPERTY_SOURCE_NAME); + attached = null; + } + if (attached == null) { sources.addFirst(new ConfigurationPropertySourcesPropertySource( ATTACHED_PROPERTY_SOURCE_NAME, new SpringConfigurationPropertySources(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 02f7b48538..1feaaaf079 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-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -56,6 +56,19 @@ public class ConfigurationPropertySourcesTests { assertThat(resolver.getProperty("server.port")).isEqualTo("1234"); } + @Test + public void attachShouldReAttachInMergedSetup() { + ConfigurableEnvironment parent = new StandardEnvironment(); + ConfigurationPropertySources.attach(parent); + parent.getProperty("my.example-property"); + ConfigurableEnvironment child = new StandardEnvironment(); + child.merge(parent); + child.getPropertySources().addLast(new MapPropertySource("config", + Collections.singletonMap("my.example_property", "1234"))); + ConfigurationPropertySources.attach(child); + assertThat(child.getProperty("my.example-property")).isEqualTo("1234"); + } + @Test public void getWhenNotAttachedShouldReturnAdapted() { ConfigurableEnvironment environment = new StandardEnvironment();