Add random.* property source
This commit is contained in:
parent
94c5203de6
commit
aeb0f0a625
|
@ -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<Random> {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in New Issue