Example usage for org.springframework.core.env MutablePropertySources replace

List of usage examples for org.springframework.core.env MutablePropertySources replace

Introduction

In this page you can find the example usage for org.springframework.core.env MutablePropertySources replace.

Prototype

public void replace(String name, PropertySource<?> propertySource) 

Source Link

Document

Replace the property source with the given name with the given property source object.

Usage

From source file:org.springframework.boot.SpringApplication.java

/**
 * Add, remove or re-order any {@link PropertySource}s in this application's
 * environment.//from   w w  w.  ja  v a2s.co m
 * @param environment this application's environment
 * @param args arguments passed to the {@code run} method
 * @see #configureEnvironment(ConfigurableEnvironment, String[])
 */
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
    MutablePropertySources sources = environment.getPropertySources();
    if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
        sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
    }
    if (this.addCommandLineProperties && args.length > 0) {
        String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
        if (sources.contains(name)) {
            PropertySource<?> source = sources.get(name);
            CompositePropertySource composite = new CompositePropertySource(name);
            composite
                    .addPropertySource(new SimpleCommandLinePropertySource(name + "-" + args.hashCode(), args));
            composite.addPropertySource(source);
            sources.replace(name, composite);
        } else {
            sources.addFirst(new SimpleCommandLinePropertySource(args));
        }
    }
}

From source file:org.springframework.cloud.bootstrap.config.ConfigServiceBootstrapConfiguration.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    CompositePropertySource composite = new CompositePropertySource("bootstrap");
    AnnotationAwareOrderComparator.sort(propertySourceLocators);
    boolean empty = true;
    for (PropertySourceLocator locator : propertySourceLocators) {
        PropertySource<?> source = null;
        try {/*w ww.  ja  v  a 2 s  .c  om*/
            source = locator.locate();
        } catch (Exception e) {
            logger.error("Could not locate PropertySource: " + e.getMessage());
        }
        if (source == null) {
            continue;
        }
        logger.info("Located property source: " + source);
        composite.addPropertySource(source);
        empty = false;
    }
    if (!empty) {
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        if (propertySources.contains("bootstrap")) {
            propertySources.replace("bootstrap", composite);
        } else {
            propertySources.addFirst(composite);
        }
    }
}

From source file:org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    CompositePropertySource composite = new CompositePropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME);
    AnnotationAwareOrderComparator.sort(propertySourceLocators);
    boolean empty = true;
    for (PropertySourceLocator locator : propertySourceLocators) {
        PropertySource<?> source = null;
        try {//from   w w w  .java  2  s .c  o  m
            source = locator.locate(applicationContext.getEnvironment());
        } catch (Exception e) {
            logger.error("Could not locate PropertySource: " + e.getMessage());
        }
        if (source == null) {
            continue;
        }
        logger.info("Located property source: " + source);
        composite.addPropertySource(source);
        empty = false;
    }
    if (!empty) {
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
            propertySources.replace(BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);
        } else {
            propertySources.addFirst(composite);
        }
    }
}

From source file:org.springframework.context.annotation.ConfigurationClassParser.java

private void addPropertySource(PropertySource<?> propertySource) {
    String name = propertySource.getName();
    MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();

    if (this.propertySourceNames.contains(name)) {
        // We've already added a version, we need to extend it
        PropertySource<?> existing = propertySources.get(name);
        if (existing != null) {
            PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource
                    ? ((ResourcePropertySource) propertySource).withResourceName()
                    : propertySource);//from ww  w  . j  a v  a 2 s  .  c  o  m
            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);
            }
            return;
        }
    }

    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);
}