Search in sources :

Example 1 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class SpringBootContextLoader method convertToPropertySources.

private PropertySources convertToPropertySources(List<String> properties) {
    Map<String, Object> source = TestPropertySourceUtils.convertInlinedPropertiesToMap(properties.toArray(new String[properties.size()]));
    MutablePropertySources sources = new MutablePropertySources();
    sources.addFirst(new MapPropertySource("inline", source));
    return sources;
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 2 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class ServerPortInfoApplicationContextInitializer method setPortProperty.

@SuppressWarnings("unchecked")
private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) {
    MutablePropertySources sources = environment.getPropertySources();
    PropertySource<?> source = sources.get("server.ports");
    if (source == null) {
        source = new MapPropertySource("server.ports", new HashMap<String, Object>());
        sources.addFirst(source);
    }
    ((Map<String, Object>) source.getSource()).put(propertyName, port);
}
Also used : HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class CloudFoundryVcapEnvironmentPostProcessor method postProcessEnvironment.

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) {
        Properties properties = new Properties();
        addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
        addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
        MutablePropertySources propertySources = environment.getPropertySources();
        if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
            propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME, new PropertiesPropertySource("vcap", properties));
        } else {
            propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
        }
    }
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties)

Example 4 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessor method deducePropertySources.

private PropertySources deducePropertySources() {
    PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
    if (configurer != null) {
        // Flatten the sources into a single list so they can be iterated
        return new FlatPropertySources(configurer.getAppliedPropertySources());
    }
    if (this.environment instanceof ConfigurableEnvironment) {
        MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
        return new FlatPropertySources(propertySources);
    }
    // empty, so not very useful, but fulfils the contract
    logger.warn("Unable to obtain PropertySources from " + "PropertySourcesPlaceholderConfigurer or Environment");
    return new MutablePropertySources();
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) PropertySourcesPlaceholderConfigurer(org.springframework.context.support.PropertySourcesPlaceholderConfigurer) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 5 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryTests method testBindWithDashPrefix.

@Test
public void testBindWithDashPrefix() throws Exception {
    // gh-4045
    this.targetName = "foo-bar";
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", Collections.<String, Object>singletonMap("FOO_BAR_NAME", "blah")));
    propertySources.addLast(new RandomValuePropertySource());
    setupFactory();
    this.factory.setPropertySources(propertySources);
    this.factory.afterPropertiesSet();
    Foo foo = this.factory.getObject();
    assertThat(foo.name).isEqualTo("blah");
}
Also used : SystemEnvironmentPropertySource(org.springframework.core.env.SystemEnvironmentPropertySource) RandomValuePropertySource(org.springframework.boot.env.RandomValuePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Aggregations

MutablePropertySources (org.springframework.core.env.MutablePropertySources)50 Test (org.junit.Test)19 MapPropertySource (org.springframework.core.env.MapPropertySource)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)8 MockPropertySource (org.springframework.mock.env.MockPropertySource)8 Properties (java.util.Properties)6 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)6 CompositePropertySource (org.springframework.core.env.CompositePropertySource)5 ByteArrayResource (org.springframework.core.io.ByteArrayResource)4 LinkedHashMap (java.util.LinkedHashMap)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 RandomValuePropertySource (org.springframework.boot.env.RandomValuePropertySource)3 PropertySource (org.springframework.core.env.PropertySource)3 StandardEnvironment (org.springframework.core.env.StandardEnvironment)3 SystemEnvironmentPropertySource (org.springframework.core.env.SystemEnvironmentPropertySource)3 MockEnvironment (org.springframework.mock.env.MockEnvironment)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2