Move CachedRandomPropertySource to EnvironmentPostProcessor

Previously it was an auto-configuration that only worked in legacy bootstrap mode.

Fixes gh-919
This commit is contained in:
spencergibb
2021-03-08 17:37:42 -05:00
parent 38a3711be7
commit da22aed5d8
5 changed files with 38 additions and 26 deletions

View File

@@ -27,7 +27,7 @@ import org.springframework.util.StringUtils;
*/
public class CachedRandomPropertySource extends PropertySource<PropertySource> {
private static final String NAME = "cachedrandom";
static final String NAME = "cachedrandom";
private static final String PREFIX = NAME + ".";

View File

@@ -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;
}
}

View File

@@ -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
org.springframework.cloud.bootstrap.encrypt.DecryptEnvironmentPostProcessor,\
org.springframework.cloud.util.random.CachedRandomPropertySourceEnvironmentPostProcessor

View File

@@ -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;

View File

@@ -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