Search in sources :

Example 1 with CompositePropertySource

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

the class PropertySourcesPropertyValuesTests method testCompositeValue.

@Test
public void testCompositeValue() {
    PropertySource<?> map = this.propertySources.get("map");
    CompositePropertySource composite = new CompositePropertySource("composite");
    composite.addPropertySource(map);
    this.propertySources.replace("map", composite);
    PropertySourcesPropertyValues propertyValues = new PropertySourcesPropertyValues(this.propertySources);
    assertThat(propertyValues.getPropertyValue("foo").getValue()).isEqualTo("bar");
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) Test(org.junit.Test)

Example 2 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project apollo by ctripcorp.

the class PropertySourcesProcessor method initializePropertySources.

private void initializePropertySources() {
    if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) {
        // already initialized
        return;
    }
    CompositePropertySource composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);
    // sort by order asc
    ImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet());
    Iterator<Integer> iterator = orders.iterator();
    while (iterator.hasNext()) {
        int order = iterator.next();
        for (String namespace : NAMESPACE_NAMES.get(order)) {
            Config config = ConfigService.getConfig(namespace);
            composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));
        }
    }
    // add after the bootstrap property source or to the first
    if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
        environment.getPropertySources().addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);
    } else {
        environment.getPropertySources().addFirst(composite);
    }
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) Config(com.ctrip.framework.apollo.Config)

Example 3 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project incubator-dubbo-spring-boot-project by apache.

the class EnvironmentUtilsTest method testExtraProperties.

@Test
public void testExtraProperties() {
    System.setProperty("user.name", "mercyblitz");
    StandardEnvironment environment = new StandardEnvironment();
    Map<String, Object> map = new HashMap<>();
    map.put("user.name", "Mercy");
    MapPropertySource propertySource = new MapPropertySource("first", map);
    CompositePropertySource compositePropertySource = new CompositePropertySource("comp");
    compositePropertySource.addFirstPropertySource(propertySource);
    MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.addFirst(compositePropertySource);
    Map<String, Object> properties = EnvironmentUtils.extractProperties(environment);
    Assert.assertEquals("Mercy", properties.get("user.name"));
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 4 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project spring-cloud-config by spring-cloud.

the class EnvironmentRepositoryPropertySourceLocator method locate.

@Override
public org.springframework.core.env.PropertySource<?> locate(Environment environment) {
    CompositePropertySource composite = new CompositePropertySource("configService");
    for (PropertySource source : repository.findOne(name, profiles, label).getPropertySources()) {
        @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource();
        composite.addPropertySource(new MapPropertySource(source.getName(), map));
    }
    return composite;
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) Map(java.util.Map) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 5 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project BroadleafCommerce by BroadleafCommerce.

the class SpringBootPropertiesTest method testProfileSourcesRegisteredOverridesCommon.

@Test
public void testProfileSourcesRegisteredOverridesCommon() {
    Assert.assertTrue(env.getPropertySources().contains(BroadleafEnvironmentConfiguringApplicationListener.FRAMEWORK_SOURCES_NAME));
    Assert.assertTrue(env.getPropertySources().contains(BroadleafEnvironmentConfiguringApplicationListener.PROFILE_AWARE_SOURCES_NAME));
    String developmentSourceName = new ClassPathResource("common-test-properties/profile-aware-properties/development.properties").getDescription();
    CompositePropertySource profileAwareSource = (CompositePropertySource) env.getPropertySources().get(BroadleafEnvironmentConfiguringApplicationListener.PROFILE_AWARE_SOURCES_NAME);
    Assert.assertTrue(profileAwareSource.getPropertySources().contains(PropertySource.named(developmentSourceName)));
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) ClassPathResource(org.springframework.core.io.ClassPathResource) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Aggregations

CompositePropertySource (org.springframework.core.env.CompositePropertySource)34 MapPropertySource (org.springframework.core.env.MapPropertySource)10 lombok.val (lombok.val)9 Test (org.junit.Test)9 MutablePropertySources (org.springframework.core.env.MutablePropertySources)9 Test (org.junit.jupiter.api.Test)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)7 HashMap (java.util.HashMap)6 IOException (java.io.IOException)4 Map (java.util.Map)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)4 PropertySource (org.springframework.core.env.PropertySource)4 StandardEnvironment (org.springframework.core.env.StandardEnvironment)4 Arrays (java.util.Arrays)3 List (java.util.List)3 FileSystemResource (org.springframework.core.io.FileSystemResource)3 Resource (org.springframework.core.io.Resource)3 Config (com.ctrip.framework.apollo.Config)2 File (java.io.File)2