Refactor YAML profile to deal with "!" profiles

Drop `SpringProfileDocumentMatcher` and replace it with two new
implementations that restrict when YAML documents are loaded. YAML
sections are now restricted both on the specific profile that is being
loaded, and the profiles that are currently accepted.

The `PropertySourceLoader` interface has been refined to include a
predicate that determines when a profile is accepted. The
`ConfigFileApplicationListener` simply delegates the predicate logic to
the `Environment`.

Fixes gh-8011
This commit is contained in:
Phillip Webb
2018-02-20 22:01:31 -08:00
parent b03fd99209
commit 4bde6b80ee
22 changed files with 542 additions and 438 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@@ -40,16 +41,17 @@ public class EnvironmentPostProcessorExample implements EnvironmentPostProcessor
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
Resource path = new ClassPathResource("com/example/myapp/config.yml");
PropertySource<?> propertySource = loadYaml(path);
PropertySource<?> propertySource = loadYaml(path, environment);
environment.getPropertySources().addLast(propertySource);
}
private PropertySource<?> loadYaml(Resource path) {
private PropertySource<?> loadYaml(Resource path, Environment environment) {
if (!path.exists()) {
throw new IllegalArgumentException("Resource " + path + " does not exist");
}
try {
return this.loader.load("custom-resource", path, null);
return this.loader.load("custom-resource", path, null,
environment::acceptsProfiles);
}
catch (IOException ex) {
throw new IllegalStateException(