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

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

Introduction

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

Prototype

public void addFirst(PropertySource<?> propertySource) 

Source Link

Document

Add the given property source object with highest precedence.

Usage

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

/**
 * Add, remove or re-order any {@link PropertySource}s in this application's
 * environment./*from  www.  j a v  a  2s.c  om*/
 * @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  w  w .  jav  a2 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 ww w .  j a  va 2s. 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.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.java

private void insert(MutablePropertySources propertySources, MapPropertySource propertySource) {
    if (propertySources.contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME, propertySource);
    } else {/*from  w  w  w  .j a  v a  2s  .  c  om*/
        propertySources.addFirst(propertySource);
    }
}

From source file:org.springframework.yarn.boot.support.YarnBootClientApplicationListener.java

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    MutablePropertySources propertySources = event.getEnvironment().getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        if (log.isDebugEnabled()) {
            log.debug("Adding afterCommandLineArgs property source after "
                    + CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME);
        }//  www  .  ja  v a 2s .  co m
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new MapPropertySource("afterCommandLineArgs", propertySourceMap));
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Adding afterCommandLineArgs property source as first");
        }
        propertySources.addFirst(new MapPropertySource("afterCommandLineArgs", propertySourceMap));
    }
}