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

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

Introduction

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

Prototype

public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) 

Source Link

Document

Add the given property source object with precedence immediately higher than the named relative property source.

Usage

From source file:com.cloudera.director.aws.common.PropertyResolvers.java

/**
 * Creates a property resolver that pulls from multiple property resource
 * locations. The first "built-in" location must be successfully loaded, but
 * all other "custom" locations must load only if {code}allowMissing{code} is
 * false./*from  w  w w .  j av  a  2s  .  co m*/
 *
 * @param allowMissing            true to allow custom resource locations to fail to load
 * @param builtInResourceLocation lowest precedence, required resource
 *                                location for properties
 * @param customResourceLocations additional resource locations for
 *                                properties, in increasing order of precedence
 * @return new property resolver
 * @throws IOException          if the built-in resource location could not be loaded,
 *                              or if {code}allowMissing{code} is false and any custom resource location
 *                              fails to load
 * @throws NullPointerException if any resource location is null
 */
public static PropertyResolver newMultiResourcePropertyResolver(boolean allowMissing,
        String builtInResourceLocation, String... customResourceLocations) throws IOException {
    MutablePropertySources sources = new MutablePropertySources();
    checkNotNull(builtInResourceLocation, "builtInResourceLocation is null");
    sources.addLast(buildPropertySource(BUILT_IN_NAME, builtInResourceLocation, false));
    String lastname = BUILT_IN_NAME;
    int customCtr = 1;
    for (String loc : customResourceLocations) {
        checkNotNull(loc, "customResourceLocations[" + (customCtr - 1) + "] is null");
        String thisname = CUSTOM_NAME_PREFIX + customCtr++;
        PropertySource source = buildPropertySource(thisname, loc, allowMissing);
        if (source != null) {
            sources.addBefore(lastname, source);
            lastname = thisname;
        }
    }

    return new PropertySourcesPropertyResolver(sources);
}

From source file:org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor.java

private void addJsonPropertySource(ConfigurableEnvironment environment, PropertySource<?> source) {
    MutablePropertySources sources = environment.getPropertySources();
    String name = findPropertySource(sources);
    if (sources.contains(name)) {
        sources.addBefore(name, source);
    } else {//  www  .  j ava  2 s.  co m
        sources.addFirst(source);
    }
}

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