Removing the possibility of null propertysources being returned in the locator

This commit is contained in:
Ryan Baxter
2019-12-13 19:37:57 -05:00
parent 763b6c1711
commit d0ca983936
2 changed files with 15 additions and 2 deletions

View File

@@ -99,7 +99,7 @@ public class PropertySourceBootstrapConfiguration implements
ConfigurableEnvironment environment = applicationContext.getEnvironment();
for (PropertySourceLocator locator : this.propertySourceLocators) {
Collection<PropertySource<?>> source = locator.locateCollection(environment);
if (source == null) {
if (source == null || source.size() == 0) {
continue;
}
List sourceList = new ArrayList<>();

View File

@@ -16,8 +16,10 @@
package org.springframework.cloud.bootstrap.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.core.env.CompositePropertySource;
@@ -43,8 +45,19 @@ public interface PropertySourceLocator {
default Collection<PropertySource<?>> locateCollection(Environment environment) {
PropertySource propertySource = locate(environment);
if (propertySource == null) {
return Collections.EMPTY_LIST;
}
if (CompositePropertySource.class.isInstance(propertySource)) {
return ((CompositePropertySource) propertySource).getPropertySources();
Collection<PropertySource<?>> sources = ((CompositePropertySource) propertySource)
.getPropertySources();
List<PropertySource<?>> filteredSources = new ArrayList<>();
for (PropertySource p : sources) {
if (p != null) {
filteredSources.add(p);
}
}
return filteredSources;
}
else {
return (List) Arrays.asList(propertySource);