Search in sources :

Example 1 with PropertiesPropertySource

use of org.springframework.core.env.PropertiesPropertySource 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 2 with PropertiesPropertySource

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

the class PropertiesConfigurationFactoryTests method bindFoo.

private Foo bindFoo(final String values) throws Exception {
    Properties properties = PropertiesLoaderUtils.loadProperties(new ByteArrayResource(values.getBytes()));
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addFirst(new PropertiesPropertySource("test", properties));
    this.factory.setPropertySources(propertySources);
    this.factory.afterPropertiesSet();
    return this.factory.getObject();
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties)

Example 3 with PropertiesPropertySource

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

the class RelaxedPropertyResolverTests method testPropertySource.

@Test
public void testPropertySource() throws Exception {
    Properties properties;
    PropertiesPropertySource propertySource;
    String propertyPrefix = "spring.datasource.";
    String propertyName = "password";
    String fullPropertyName = propertyPrefix + propertyName;
    StandardEnvironment environment = new StandardEnvironment();
    MutablePropertySources sources = environment.getPropertySources();
    properties = new Properties();
    properties.put(fullPropertyName, "systemPassword");
    propertySource = new PropertiesPropertySource("system", properties);
    sources.addLast(propertySource);
    properties = new Properties();
    properties.put(fullPropertyName, "propertiesPassword");
    propertySource = new PropertiesPropertySource("properties", properties);
    sources.addLast(propertySource);
    RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, propertyPrefix);
    String directProperty = propertyResolver.getProperty(propertyName);
    Map<String, Object> subProperties = propertyResolver.getSubProperties("");
    String subProperty = (String) subProperties.get(propertyName);
    assertThat(subProperty).isEqualTo(directProperty);
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 4 with PropertiesPropertySource

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

the class PropertiesConfigurationFactoryMapTests method bindFoo.

private Foo bindFoo(final String values) throws Exception {
    Properties properties = PropertiesLoaderUtils.loadProperties(new ByteArrayResource(values.getBytes()));
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addFirst(new PropertiesPropertySource("test", properties));
    this.factory.setPropertySources(propertySources);
    this.factory.afterPropertiesSet();
    return this.factory.getObject();
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties)

Example 5 with PropertiesPropertySource

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

the class PropertiesConfigurationFactoryParameterizedTests method bindFoo.

private Foo bindFoo(final String values) throws Exception {
    Properties properties = PropertiesLoaderUtils.loadProperties(new ByteArrayResource(values.getBytes()));
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addFirst(new PropertiesPropertySource("test", properties));
    this.factory.setPropertySources(propertySources);
    this.factory.afterPropertiesSet();
    return this.factory.getObject();
}
Also used : PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Properties(java.util.Properties)

Aggregations

PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)16 Properties (java.util.Properties)13 MutablePropertySources (org.springframework.core.env.MutablePropertySources)8 File (java.io.File)3 IOException (java.io.IOException)3 ByteArrayResource (org.springframework.core.io.ByteArrayResource)3 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)2 FileInputStream (java.io.FileInputStream)1 PostConstruct (javax.annotation.PostConstruct)1 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)1 CasVersion (org.apereo.cas.util.CasVersion)1 Before (org.junit.Before)1 Test (org.junit.Test)1 PropertyValues (org.springframework.beans.PropertyValues)1 PropertySourcesPropertyValues (org.springframework.boot.bind.PropertySourcesPropertyValues)1 RelaxedDataBinder (org.springframework.boot.bind.RelaxedDataBinder)1 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)1 BuildProperties (org.springframework.boot.info.BuildProperties)1