From 624be9d19ed99fb2425db531de5ceaab1ba68830 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 15 Feb 2016 10:25:19 +0000 Subject: [PATCH] Fix ordering of property sources When the bootstrap properties are added back into the main Environment we have to be careful that any source that is already there (such as one that was added in a Spring Boot EnvironmentPostProcessor) is not replaced in the wrong order. Fixes gh-90 --- .../BootstrapApplicationListener.java | 6 ++- ...SpringApplicationJsonIntegrationTests.java | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingSpringApplicationJsonIntegrationTests.java diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java index 1f23cbfa..b3ec9f3b 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java @@ -302,8 +302,10 @@ public class BootstrapApplicationListener DEFAULT_PROPERTIES, defaultProperties.getSource())); for (PropertySource source : defaultProperties.getPropertySources() .getPropertySources()) { - environment.getPropertySources().addBefore(DEFAULT_PROPERTIES, - source); + if (!environment.getPropertySources().contains(source.getName())) { + environment.getPropertySources().addBefore(DEFAULT_PROPERTIES, + source); + } } } } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingSpringApplicationJsonIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingSpringApplicationJsonIntegrationTests.java new file mode 100644 index 00000000..18fc13ab --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingSpringApplicationJsonIntegrationTests.java @@ -0,0 +1,49 @@ +package org.springframework.cloud.bootstrap; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.bootstrap.BootstrapOrderingSpringApplicationJsonIntegrationTests.Application; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@IntegrationTest("spring.cloud.bootstrap.name:json") +public class BootstrapOrderingSpringApplicationJsonIntegrationTests { + + @BeforeClass + public static void spikeJson() { + System.setProperty("SPRING_APPLICATION_JSON", "{\"message\":\"From JSON\"}"); + } + + @AfterClass + public static void unspikeJson() { + System.clearProperty("SPRING_APPLICATION_JSON"); + } + + @Autowired + private ConfigurableEnvironment environment; + + @Test + public void bootstrapPropertiesExist() { + assertTrue(this.environment.getPropertySources() + .contains("spring.application.json")); + assertEquals("From JSON", this.environment.getProperty("message")); + } + + @EnableAutoConfiguration + @Configuration + protected static class Application { + } + +}