Search in sources :

Example 11 with CompositePropertySource

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

the class DefaultPropertiesPropertySourceTests method addOrMergeWhenExistingNotMapPropertySourceShouldNotMerge.

@Test
void addOrMergeWhenExistingNotMapPropertySourceShouldNotMerge() {
    MockEnvironment environment = new MockEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    CompositePropertySource composite = new CompositePropertySource(DefaultPropertiesPropertySource.NAME);
    composite.addPropertySource(new DefaultPropertiesPropertySource(Collections.singletonMap("spring", "boot")));
    propertySources.addFirst(composite);
    DefaultPropertiesPropertySource.addOrMerge(Collections.singletonMap("hello", "world"), propertySources);
    assertThat(propertySources.contains(DefaultPropertiesPropertySource.NAME)).isTrue();
    assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("spring")).isNull();
    assertThat(propertySources.get(DefaultPropertiesPropertySource.NAME).getProperty("hello")).isEqualTo("world");
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) MockEnvironment(org.springframework.mock.env.MockEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.jupiter.api.Test)

Example 12 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project dubbo by alibaba.

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 13 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project cas by apereo.

the class StandaloneConfigurationFilePropertiesSourceLocator method locate.

@Override
public Optional<PropertySource<?>> locate(final Environment environment, final ResourceLoader resourceLoader) {
    val compositePropertySource = new CompositePropertySource(getClass().getSimpleName());
    val configFile = casConfigurationPropertiesEnvironmentManager.getStandaloneProfileConfigurationFile(environment);
    if (configFile != null) {
        val sourceStandalone = loadSettingsFromStandaloneConfigFile(configFile);
        compositePropertySource.addPropertySource(sourceStandalone);
        return Optional.of(compositePropertySource);
    }
    return Optional.empty();
}
Also used : lombok.val(lombok.val) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Example 14 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project cas by apereo.

the class DefaultCasConfigurationPropertiesSourceLocator method loadSettingsByApplicationProfiles.

/**
 * Property files processed in order of non-profiles first and then profiles, and profiles are first to last
 * with properties in the last profile overriding properties in previous profiles or non-profiles.
 *
 * @param environment Spring environment
 * @param config      Location of config files
 * @return Merged properties
 */
private PropertySource<?> loadSettingsByApplicationProfiles(final Environment environment, final File config) {
    val profiles = ConfigurationPropertiesLoaderFactory.getApplicationProfiles(environment);
    val resources = scanForConfigurationResources(environment, config, profiles);
    val composite = new CompositePropertySource("applicationProfilesCompositeProperties");
    LOGGER.info("Configuration files found at [{}] are [{}] under profile(s) [{}]", config, resources, profiles);
    resources.forEach(Unchecked.consumer(f -> {
        LOGGER.debug("Loading configuration file [{}]", f);
        val loader = configurationPropertiesLoaderFactory.getLoader(f, "applicationProfilesProperties-" + f.getFilename());
        composite.addFirstPropertySource(loader.load());
    }));
    return composite;
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Unchecked(org.jooq.lambda.Unchecked) ResourceLoader(org.springframework.core.io.ResourceLoader) PropertySource(org.springframework.core.env.PropertySource) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) FileSystemResource(org.springframework.core.io.FileSystemResource) Collectors(java.util.stream.Collectors) ConfigurationPropertiesLoaderFactory(org.apereo.cas.configuration.loader.ConfigurationPropertiesLoaderFactory) File(java.io.File) CompositePropertySource(org.springframework.core.env.CompositePropertySource) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Environment(org.springframework.core.env.Environment) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) CasConfigurationPropertiesSourceLocator(org.apereo.cas.configuration.api.CasConfigurationPropertiesSourceLocator) Resource(org.springframework.core.io.Resource) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Example 15 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project cas by apereo.

the class DefaultCasConfigurationPropertiesSourceLocator method locate.

/**
 * Adding items to composite property source which contains
 * property sources processed in order, first one wins.
 * First Priority: Configuration files in config dir,
 * profiles override non-profiles, last profile overrides first
 * Second Priority: {@code classpath:/application.yml}
 *
 * @param environment    the environment
 * @param resourceLoader the resource loader
 * @return CompositePropertySource containing sources listed above
 */
@Override
public Optional<PropertySource<?>> locate(final Environment environment, final ResourceLoader resourceLoader) {
    val compositePropertySource = new CompositePropertySource("casCompositePropertySource");
    val config = casConfigurationPropertiesEnvironmentManager.getStandaloneProfileConfigurationDirectory(environment);
    LOGGER.debug("Located CAS standalone configuration directory at [{}]", config);
    if (config != null && config.isDirectory() && config.exists()) {
        val sourceProfiles = loadSettingsByApplicationProfiles(environment, config);
        compositePropertySource.addPropertySource(sourceProfiles);
    } else {
        LOGGER.info("Configuration directory [{}] is not a directory or cannot be found at the specific path", config);
    }
    val sourceYaml = loadEmbeddedYamlOverriddenProperties(resourceLoader, environment);
    compositePropertySource.addPropertySource(sourceYaml);
    return Optional.of(compositePropertySource);
}
Also used : lombok.val(lombok.val) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Aggregations

CompositePropertySource (org.springframework.core.env.CompositePropertySource)35 MapPropertySource (org.springframework.core.env.MapPropertySource)11 Test (org.junit.Test)10 MutablePropertySources (org.springframework.core.env.MutablePropertySources)10 lombok.val (lombok.val)9 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)6 StandardEnvironment (org.springframework.core.env.StandardEnvironment)5 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 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