diff --git a/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java index 32944967ac..5ba1effc2f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import org.springframework.beans.PropertyValues; @@ -40,10 +41,12 @@ import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; +import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.util.ClassUtils; +import org.springframework.util.DigestUtils; import org.springframework.util.StringUtils; /** @@ -106,6 +109,9 @@ public class ConfigFileApplicationContextInitializer implements if (this.environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment environment = (ConfigurableEnvironment) this.environment; load(environment, new DefaultResourceLoader()); + environment.getPropertySources().addAfter( + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, + new RandomValuePropertySource("random")); // Set bean properties from the early environment PropertyValues propertyValues = new PropertySourcesPropertyValues( environment.getPropertySources()); @@ -251,4 +257,28 @@ public class ConfigFileApplicationContextInitializer implements this.searchLocations = (searchLocations == null ? null : searchLocations.clone()); } + private static class RandomValuePropertySource extends PropertySource { + + public RandomValuePropertySource(String name) { + super(name, new Random()); + } + + @Override + public Object getProperty(String name) { + if (!name.startsWith("random.")) { + return null; + } + if (name.endsWith("int")) { + return getSource().nextInt(); + } + if (name.endsWith("long")) { + return getSource().nextLong(); + } + byte[] bytes = new byte[32]; + getSource().nextBytes(bytes); + return DigestUtils.md5DigestAsHex(bytes); + } + + } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java index 7bf9b05a2b..c0e34df54e 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java @@ -21,11 +21,14 @@ import java.util.Map; import org.junit.After; import org.junit.Test; +import org.springframework.boot.SpringApplication; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource; +import org.springframework.core.env.StandardEnvironment; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; @@ -54,6 +57,15 @@ public class ConfigFileApplicationContextInitializerTests { assertThat(property, equalTo("frompropertiesfile")); } + @Test + public void randomValue() throws Exception { + StandardEnvironment environment = new StandardEnvironment(); + this.initializer.setEnvironment(environment); + this.initializer.initialize(new SpringApplication(), new String[0]); + String property = environment.getProperty("random.value"); + assertThat(property, notNullValue()); + } + @Test public void loadTwoPropertiesFiles() throws Exception { this.initializer.setNames("testproperties,moreproperties");