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

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

Introduction

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

Prototype

String resolveRequiredPlaceholders(String text) throws IllegalArgumentException;

Source Link

Document

Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by #getProperty .

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}./*  w w w. j  a  v  a 2s. c  o  m*/
 * <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);
    }
}