Add constants for well-known PropertySource names

Closes gh-4776
pull/4690/merge
Stephane Nicoll 9 years ago
parent 543a746de7
commit 9be4b57182

@ -131,6 +131,11 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
*/
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
/**
* Name of the application configuration {@link PropertySource}.
*/
public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
private final DeferredLog logger = new DeferredLog();
private String searchLocations;
@ -599,14 +604,14 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
static class ConfigurationPropertySources
extends EnumerablePropertySource<Collection<PropertySource<?>>> {
private static final String NAME = "applicationConfigurationProperties";
private final Collection<PropertySource<?>> sources;
private final String[] names;
ConfigurationPropertySources(Collection<PropertySource<?>> sources) {
super(NAME, sources);
super(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME, sources);
this.sources = sources;
List<String> names = new ArrayList<String>();
for (PropertySource<?> source : sources) {
@ -630,9 +635,9 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
}
public static void finishAndRelocate(MutablePropertySources propertySources) {
String name = APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME;
ConfigurationPropertySources removed = (ConfigurationPropertySources) propertySources
.get(ConfigurationPropertySources.NAME);
String name = ConfigurationPropertySources.NAME;
.get(name);
if (removed != null) {
for (PropertySource<?> propertySource : removed.sources) {
if (propertySource instanceof EnumerableCompositePropertySource) {
@ -646,7 +651,7 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
propertySources.addAfter(name, propertySource);
}
}
propertySources.remove(ConfigurationPropertySources.NAME);
propertySources.remove(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME);
}
}

@ -51,6 +51,11 @@ import org.springframework.util.StringUtils;
*/
public class RandomValuePropertySource extends PropertySource<Random> {
/**
* Name of the random {@link PropertySource}.
*/
public static final String RANDOM_PROPERTY_SOURCE_NAME = "random";
private static final String PREFIX = "random.";
private static Log logger = LogFactory.getLog(RandomValuePropertySource.class);
@ -59,6 +64,10 @@ public class RandomValuePropertySource extends PropertySource<Random> {
super(name, new Random());
}
public RandomValuePropertySource() {
this(RANDOM_PROPERTY_SOURCE_NAME);
}
@Override
public Object getProperty(String name) {
if (!name.startsWith(PREFIX)) {
@ -126,7 +135,7 @@ public class RandomValuePropertySource extends PropertySource<Random> {
public static void addToEnvironment(ConfigurableEnvironment environment) {
environment.getPropertySources().addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
new RandomValuePropertySource("random"));
new RandomValuePropertySource(RANDOM_PROPERTY_SOURCE_NAME));
logger.trace("RandomValuePropertySource add to Environment");
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -126,7 +126,7 @@ public class PropertiesConfigurationFactoryTests {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment",
Collections.<String, Object>singletonMap("FOO_BAR_NAME", "blah")));
propertySources.addLast(new RandomValuePropertySource("random"));
propertySources.addLast(new RandomValuePropertySource());
setupFactory();
this.factory.setPropertySources(propertySources);
this.factory.afterPropertiesSet();

@ -519,7 +519,8 @@ public class ConfigFileApplicationListenerTests {
assertThat(Arrays.asList(this.environment.getActiveProfiles()), contains("dev"));
assertThat(property, equalTo("fromdevprofile"));
ConfigurationPropertySources propertySource = (ConfigurationPropertySources) this.environment
.getPropertySources().get("applicationConfigurationProperties");
.getPropertySources()
.get(ConfigFileApplicationListener.APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME);
Collection<org.springframework.core.env.PropertySource<?>> sources = propertySource
.getSource();
assertEquals(2, sources.size());

@ -33,7 +33,7 @@ import static org.junit.Assert.assertTrue;
*/
public class RandomValuePropertySourceTests {
private RandomValuePropertySource source = new RandomValuePropertySource("random");
private RandomValuePropertySource source = new RandomValuePropertySource();
@Test
public void notRandom() {

Loading…
Cancel
Save