diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySource.java b/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySource.java index bbb60d05..ff678ebc 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySource.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySource.java @@ -27,7 +27,7 @@ import org.springframework.util.StringUtils; */ public class CachedRandomPropertySource extends PropertySource { - private static final String NAME = "cachedrandom"; + static final String NAME = "cachedrandom"; private static final String PREFIX = NAME + "."; diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySourceAutoConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySourceEnvironmentPostProcessor.java similarity index 51% rename from spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySourceAutoConfiguration.java rename to spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySourceEnvironmentPostProcessor.java index aeb3dc91..5e359f50 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySourceAutoConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/util/random/CachedRandomPropertySourceEnvironmentPostProcessor.java @@ -16,11 +16,15 @@ package org.springframework.cloud.util.random; -import javax.annotation.PostConstruct; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.RandomValuePropertySource; +import org.springframework.boot.env.RandomValuePropertySourceEnvironmentPostProcessor; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; @@ -29,18 +33,28 @@ import org.springframework.core.env.PropertySource; * @author Ryan Baxter */ @Configuration(proxyBeanMethods = false) -public class CachedRandomPropertySourceAutoConfiguration { +public class CachedRandomPropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { - @Autowired - ConfigurableEnvironment environment; + private static final Log logger = LogFactory.getLog(CachedRandomPropertySourceEnvironmentPostProcessor.class); - @PostConstruct - public void initialize() { + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { MutablePropertySources propertySources = environment.getPropertySources(); - PropertySource propertySource = propertySources.get(RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME); + PropertySource propertySource = propertySources.get(RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME); if (propertySource != null) { + PropertySource existing = propertySources.get(CachedRandomPropertySource.NAME); + if (existing != null) { + logger.trace("CachedRandomPropertySource already present"); + return; + } propertySources.addLast(new CachedRandomPropertySource(propertySource)); + logger.trace("CachedRandomPropertySource added to Environment"); } } + @Override + public int getOrder() { + return RandomValuePropertySourceEnvironmentPostProcessor.ORDER + 1; + } + } diff --git a/spring-cloud-context/src/main/resources/META-INF/spring.factories b/spring-cloud-context/src/main/resources/META-INF/spring.factories index 16deea63..85783673 100644 --- a/spring-cloud-context/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-context/src/main/resources/META-INF/spring.factories @@ -15,11 +15,11 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\ org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\ org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\ -org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\ -org.springframework.cloud.util.random.CachedRandomPropertySourceAutoConfiguration +org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration # Spring Boot Bootstrappers org.springframework.boot.Bootstrapper=\ org.springframework.cloud.bootstrap.TextEncryptorConfigBootstrapper # Environment Post Processors org.springframework.boot.env.EnvironmentPostProcessor=\ -org.springframework.cloud.bootstrap.encrypt.DecryptEnvironmentPostProcessor \ No newline at end of file +org.springframework.cloud.bootstrap.encrypt.DecryptEnvironmentPostProcessor,\ +org.springframework.cloud.util.random.CachedRandomPropertySourceEnvironmentPostProcessor diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresherIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresherIntegrationTests.java index 39e77707..6d902449 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresherIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresherIntegrationTests.java @@ -25,7 +25,6 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.boot.SpringApplication; @@ -121,14 +120,13 @@ public class ConfigDataContextRefresherIntegrationTests { } @Test - @Disabled public void testCachedRandom() { - // long cachedRandomLong = properties.getCachedRandomLong(); + long cachedRandomLong = properties.getCachedRandomLong(); long randomLong = properties.randomLong(); - // then(cachedRandomLong).isNotNull(); + then(cachedRandomLong).isNotNull(); this.refresher.refresh(); then(randomLong).isNotEqualTo(properties.randomLong()); - // then(cachedRandomLong).isEqualTo(properties.cachedRandomLong); + then(cachedRandomLong).isEqualTo(properties.cachedRandomLong); } protected static class TestEnvPostProcessor implements EnvironmentPostProcessor, Ordered { @@ -222,7 +220,7 @@ public class ConfigDataContextRefresherIntegrationTests { private int delay; - // private Long cachedRandomLong; + private Long cachedRandomLong; private Long randomLong; @@ -242,12 +240,13 @@ public class ConfigDataContextRefresherIntegrationTests { this.delay = delay; } - /* - * public long getCachedRandomLong() { return cachedRandomLong; } - * - * public void setCachedRandomLong(long cachedRandomLong) { this.cachedRandomLong - * = cachedRandomLong; } - */ + public Long getCachedRandomLong() { + return this.cachedRandomLong; + } + + public void setCachedRandomLong(Long cachedRandomLong) { + this.cachedRandomLong = cachedRandomLong; + } public long randomLong() { return randomLong; diff --git a/spring-cloud-context/src/test/resources/META-INF/spring.factories b/spring-cloud-context/src/test/resources/META-INF/spring.factories index 0dacb8dc..86d51e5a 100644 --- a/spring-cloud-context/src/test/resources/META-INF/spring.factories +++ b/spring-cloud-context/src/test/resources/META-INF/spring.factories @@ -1,8 +1,7 @@ # Bootstrap components org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.bootstrap.TestBootstrapConfiguration,\ -org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration,\ -org.springframework.cloud.util.random.CachedRandomPropertySourceAutoConfiguration +org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration org.springframework.boot.env.EnvironmentPostProcessor=\ org.springframework.cloud.context.refresh.ConfigDataContextRefresherIntegrationTests.TestEnvPostProcessor