Rationalize multi-document config file handling
Update `PropertySourceLoader` so that it no longer needs to deal with matching multi-document files using the `spring.profile` property. The loader now simply returns one or more `PropertSource` instances for a given `Resource`. All property matching now occurs in the `ConfigFileApplicationListener`. This allows document processing logic to be contained in a single place, and allows us to rationalize the algorithm so that negative matching profiles are processed last. Fixes gh-12159
This commit is contained in:
@@ -22,7 +22,6 @@ 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;
|
||||
@@ -41,17 +40,16 @@ 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, environment);
|
||||
PropertySource<?> propertySource = loadYaml(path);
|
||||
environment.getPropertySources().addLast(propertySource);
|
||||
}
|
||||
|
||||
private PropertySource<?> loadYaml(Resource path, Environment environment) {
|
||||
private PropertySource<?> loadYaml(Resource path) {
|
||||
if (!path.exists()) {
|
||||
throw new IllegalArgumentException("Resource " + path + " does not exist");
|
||||
}
|
||||
try {
|
||||
return this.loader.load("custom-resource", path, null,
|
||||
environment::acceptsProfiles);
|
||||
return this.loader.load("custom-resource", path).get(0);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(
|
||||
|
||||
Reference in New Issue
Block a user