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");
}
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"));
}
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();
}
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;
}
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);
}
Aggregations