Example usage for org.springframework.core.env ConfigurableEnvironment getPropertySources

List of usage examples for org.springframework.core.env ConfigurableEnvironment getPropertySources

Introduction

In this page you can find the example usage for org.springframework.core.env ConfigurableEnvironment getPropertySources.

Prototype

MutablePropertySources getPropertySources();

Source Link

Document

Return the PropertySources for this Environment in mutable form, allowing for manipulation of the set of PropertySource objects that should be searched when resolving properties against this Environment object.

Usage

From source file:org.springframework.test.context.support.TestPropertySourceUtils.java

/**
 * Add the {@link Properties} files from the given resource {@code locations}
 * to the supplied {@link ConfigurableEnvironment environment}.
 * <p>Property placeholders in resource locations (i.e., <code>${...}</code>)
 * will be {@linkplain Environment#resolveRequiredPlaceholders(String) resolved}
 * against the {@code Environment}./*from   ww  w  .  j a  va2s. c om*/
 * <p>Each properties file will be converted to a {@link ResourcePropertySource}
 * that will be added to the {@link PropertySources} of the environment with
 * highest precedence.
 * @param environment the environment to update; never {@code null}
 * @param resourceLoader the {@code ResourceLoader} to use to load each resource;
 * never {@code null}
 * @param locations the resource locations of {@code Properties} files to add
 * to the environment; potentially empty but never {@code null}
 * @since 4.3
 * @see ResourcePropertySource
 * @see TestPropertySource#locations
 * @see #addPropertiesFilesToEnvironment(ConfigurableApplicationContext, String...)
 * @throws IllegalStateException if an error occurs while processing a properties file
 */
public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment,
        ResourceLoader resourceLoader, String... locations) {

    Assert.notNull(environment, "'environment' must not be null");
    Assert.notNull(resourceLoader, "'resourceLoader' must not be null");
    Assert.notNull(locations, "'locations' must not be null");
    try {
        for (String location : locations) {
            String resolvedLocation = environment.resolveRequiredPlaceholders(location);
            Resource resource = resourceLoader.getResource(resolvedLocation);
            environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
        }
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to add PropertySource to Environment", ex);
    }
}

From source file:org.springframework.test.context.support.TestPropertySourceUtils.java

/**
 * Add the given <em>inlined properties</em> (in the form of <em>key-value</em>
 * pairs) to the supplied {@link ConfigurableEnvironment environment}.
 * <p>All key-value pairs will be added to the {@code Environment} as a
 * single {@link MapPropertySource} with the highest precedence.
 * <p>For details on the parsing of <em>inlined properties</em>, consult the
 * Javadoc for {@link #convertInlinedPropertiesToMap}.
 * @param environment the environment to update; never {@code null}
 * @param inlinedProperties the inlined properties to add to the environment;
 * potentially empty but never {@code null}
 * @since 4.1.5/*from   w ww.jav a  2s . c  om*/
 * @see MapPropertySource
 * @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
 * @see TestPropertySource#properties
 * @see #convertInlinedPropertiesToMap
 */
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment,
        String... inlinedProperties) {
    Assert.notNull(environment, "'environment' must not be null");
    Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
    if (!ObjectUtils.isEmpty(inlinedProperties)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Adding inlined properties to environment: "
                    + ObjectUtils.nullSafeToString(inlinedProperties));
        }
        MapPropertySource ps = (MapPropertySource) environment.getPropertySources()
                .get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
        if (ps == null) {
            ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>());
            environment.getPropertySources().addFirst(ps);
        }
        ps.getSource().putAll(convertInlinedPropertiesToMap(inlinedProperties));
    }
}

From source file:org.springframework.xd.dirt.plugins.spark.streaming.SparkStreamingPlugin.java

/**
 * Get the configured message bus properties for the given transport.
 * @param module/*from w w w  . j  a  va  2  s.  co m*/
 * @return the message bus properties for the spark streaming module.
 */
private Properties getMessageBusProperties(Module module) {
    ConfigurableEnvironment env = module.getApplicationContext().getEnvironment();
    Properties busProperties = new Properties();
    busProperties.put("XD_TRANSPORT", env.getProperty("XD_TRANSPORT"));
    Iterator<PropertySource<?>> i = env.getPropertySources().iterator();
    while (i.hasNext()) {
        PropertySource<?> p = i.next();
        if (p instanceof EnumerablePropertySource) {
            for (String name : ((EnumerablePropertySource) p).getPropertyNames()) {
                if ((name.startsWith(REDIS_CONNECTION_PROPERTY_PREFIX))
                        || name.startsWith(RABBIT_CONNECTION_PROPERTY_PREFIX)
                        || name.startsWith(MESSAGE_BUS_PROPERTY_PREFIX)) {
                    busProperties.put(name, env.getProperty(name));
                }
            }
        }
    }
    return busProperties;
}