Search in sources :

Example 6 with CompositePropertySource

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

the class ConfigurationClassParser method addPropertySource.

private void addPropertySource(PropertySource<?> propertySource) {
    String name = propertySource.getName();
    MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
    if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
        // We've already added a version, we need to extend it
        PropertySource<?> existing = propertySources.get(name);
        PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource);
        if (existing instanceof CompositePropertySource) {
            ((CompositePropertySource) existing).addFirstPropertySource(newSource);
        } else {
            if (existing instanceof ResourcePropertySource) {
                existing = ((ResourcePropertySource) existing).withResourceName();
            }
            CompositePropertySource composite = new CompositePropertySource(name);
            composite.addPropertySource(newSource);
            composite.addPropertySource(existing);
            propertySources.replace(name, composite);
        }
    } else {
        if (this.propertySourceNames.isEmpty()) {
            propertySources.addLast(propertySource);
        } else {
            String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
            propertySources.addBefore(firstProcessed, propertySource);
        }
    }
    this.propertySourceNames.add(name);
}
Also used : ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Aggregations

CompositePropertySource (org.springframework.core.env.CompositePropertySource)6 Test (org.junit.Test)3 MapPropertySource (org.springframework.core.env.MapPropertySource)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 Map (java.util.Map)1 SimpleCommandLinePropertySource (org.springframework.core.env.SimpleCommandLinePropertySource)1 ResourcePropertySource (org.springframework.core.io.support.ResourcePropertySource)1