use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.
the class ConfigurationClassParser method addPropertySource.
private void addPropertySource(PropertySource<?> propertySource) {
String name = propertySource.getName();
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
// We've already added a version, we need to extend it
PropertySource<?> existing = propertySources.get(name);
PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource);
if (existing instanceof CompositePropertySource) {
((CompositePropertySource) existing).addFirstPropertySource(newSource);
} else {
if (existing instanceof ResourcePropertySource) {
existing = ((ResourcePropertySource) existing).withResourceName();
}
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(newSource);
composite.addPropertySource(existing);
propertySources.replace(name, composite);
}
} else {
if (this.propertySourceNames.isEmpty()) {
propertySources.addLast(propertySource);
} else {
String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
propertySources.addBefore(firstProcessed, propertySource);
}
}
this.propertySourceNames.add(name);
}
use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.
the class PropertySourceAnnotationTests method withExplicitName.
@Test
public void withExplicitName() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithExplicitName.class);
ctx.refresh();
assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("p1"));
assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
// assert that the property source was added last to the set of sources
String name;
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator();
do {
name = iterator.next().getName();
} while (iterator.hasNext());
assertThat(name, is("p1"));
}
use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.
the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesLocalProperties.
@Test
@SuppressWarnings("serial")
public void explicitPropertySourcesExcludesLocalProperties() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(new MockPropertySource());
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setPropertySources(propertySources);
ppc.setProperties(new Properties() {
{
put("my.name", "local");
}
});
ppc.setIgnoreUnresolvablePlaceholders(true);
ppc.postProcessBeanFactory(bf);
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
}
use of org.springframework.core.env.MutablePropertySources in project spring-framework by spring-projects.
the class TestPropertySourceUtilsTests method addInlinedPropertiesToEnvironmentWithEmptyProperty.
@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
ConfigurableEnvironment environment = new MockEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
assertEquals(0, propertySources.size());
addInlinedPropertiesToEnvironment(environment, asArray(" "));
assertEquals(1, propertySources.size());
assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.
the class SpringApplicationJsonEnvironmentPostProcessor method addJsonPropertySource.
private void addJsonPropertySource(ConfigurableEnvironment environment, PropertySource<?> source) {
MutablePropertySources sources = environment.getPropertySources();
String name = findPropertySource(sources);
if (sources.contains(name)) {
sources.addBefore(name, source);
} else {
sources.addFirst(source);
}
}
Aggregations