|
56 | 56 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
57 | 57 | import org.springframework.core.env.ConfigurableEnvironment;
|
58 | 58 | import org.springframework.core.env.Environment;
|
59 |
| -import org.springframework.core.env.MapPropertySource; |
60 | 59 | import org.springframework.core.env.MutablePropertySources;
|
61 | 60 | import org.springframework.core.env.Profiles;
|
62 | 61 | import org.springframework.core.env.PropertySource;
|
@@ -117,6 +116,15 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
|
117 | 116 |
|
118 | 117 | private static final Bindable<List<String>> STRING_LIST = Bindable.listOf(String.class);
|
119 | 118 |
|
| 119 | + private static final Set<String> LOAD_FILTERED_PROPERTY; |
| 120 | + |
| 121 | + static { |
| 122 | + Set<String> filteredProperties = new HashSet<>(); |
| 123 | + filteredProperties.add("spring.profiles.active"); |
| 124 | + filteredProperties.add("spring.profiles.include"); |
| 125 | + LOAD_FILTERED_PROPERTY = Collections.unmodifiableSet(filteredProperties); |
| 126 | + } |
| 127 | + |
120 | 128 | /**
|
121 | 129 | * The "active profiles" property name.
|
122 | 130 | */
|
@@ -311,32 +319,26 @@ private class Loader {
|
311 | 319 | }
|
312 | 320 |
|
313 | 321 | public void load() {
|
314 |
| - this.profiles = new LinkedList<>(); |
315 |
| - this.processedProfiles = new LinkedList<>(); |
316 |
| - this.activatedProfiles = false; |
317 |
| - this.loaded = new LinkedHashMap<>(); |
318 |
| - MapPropertySource defaultProperties = (MapPropertySource) this.environment.getPropertySources() |
319 |
| - .get(DEFAULT_PROPERTIES); |
320 |
| - replaceDefaultPropertySourceIfNecessary(defaultProperties); |
321 |
| - initializeProfiles(); |
322 |
| - while (!this.profiles.isEmpty()) { |
323 |
| - Profile profile = this.profiles.poll(); |
324 |
| - if (profile != null && !profile.isDefaultProfile()) { |
325 |
| - addProfileToEnvironment(profile.getName()); |
326 |
| - } |
327 |
| - load(profile, this::getPositiveProfileFilter, addToLoaded(MutablePropertySources::addLast, false)); |
328 |
| - this.processedProfiles.add(profile); |
329 |
| - } |
330 |
| - load(null, this::getNegativeProfileFilter, addToLoaded(MutablePropertySources::addFirst, true)); |
331 |
| - addLoadedPropertySources(); |
332 |
| - resetEnvironment(defaultProperties); |
333 |
| - } |
334 |
| - |
335 |
| - private void replaceDefaultPropertySourceIfNecessary(MapPropertySource defaultProperties) { |
336 |
| - if (defaultProperties != null) { |
337 |
| - this.environment.getPropertySources().replace(DEFAULT_PROPERTIES, |
338 |
| - new FilteredDefaultPropertySource(DEFAULT_PROPERTIES, defaultProperties.getSource())); |
339 |
| - } |
| 322 | + FilteredPropertySource.apply(this.environment, DEFAULT_PROPERTIES, LOAD_FILTERED_PROPERTY, |
| 323 | + (defaultProperties) -> { |
| 324 | + this.profiles = new LinkedList<>(); |
| 325 | + this.processedProfiles = new LinkedList<>(); |
| 326 | + this.activatedProfiles = false; |
| 327 | + this.loaded = new LinkedHashMap<>(); |
| 328 | + initializeProfiles(); |
| 329 | + while (!this.profiles.isEmpty()) { |
| 330 | + Profile profile = this.profiles.poll(); |
| 331 | + if (isDefaultProfile(profile)) { |
| 332 | + addProfileToEnvironment(profile.getName()); |
| 333 | + } |
| 334 | + load(profile, this::getPositiveProfileFilter, |
| 335 | + addToLoaded(MutablePropertySources::addLast, false)); |
| 336 | + this.processedProfiles.add(profile); |
| 337 | + } |
| 338 | + load(null, this::getNegativeProfileFilter, addToLoaded(MutablePropertySources::addFirst, true)); |
| 339 | + addLoadedPropertySources(); |
| 340 | + applyActiveProfiles(defaultProperties); |
| 341 | + }); |
340 | 342 | }
|
341 | 343 |
|
342 | 344 | /**
|
@@ -688,68 +690,27 @@ private void addLoadedPropertySource(MutablePropertySources destination, String
|
688 | 690 | }
|
689 | 691 | }
|
690 | 692 |
|
691 |
| - private void resetEnvironment(MapPropertySource defaultProperties) { |
| 693 | + private void applyActiveProfiles(PropertySource<?> defaultProperties) { |
692 | 694 | List<String> activeProfiles = new ArrayList<>();
|
693 |
| - handleDefaultPropertySource(defaultProperties, activeProfiles); |
694 |
| - activeProfiles.addAll( |
695 |
| - this.processedProfiles.stream().filter((profile) -> profile != null && !profile.isDefaultProfile()) |
696 |
| - .map(Profile::getName).collect(Collectors.toList())); |
697 |
| - this.environment.setActiveProfiles(activeProfiles.toArray(new String[0])); |
698 |
| - } |
699 |
| - |
700 |
| - private void handleDefaultPropertySource(MapPropertySource defaultProperties, List<String> activeProfiles) { |
701 | 695 | if (defaultProperties != null) {
|
702 | 696 | Binder binder = new Binder(ConfigurationPropertySources.from(defaultProperties),
|
703 | 697 | new PropertySourcesPlaceholdersResolver(this.environment));
|
704 |
| - List<String> includes = getDefaultProfiles(binder, "spring.profiles.include"); |
705 |
| - activeProfiles.addAll(includes); |
| 698 | + activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.include")); |
706 | 699 | if (!this.activatedProfiles) {
|
707 |
| - List<String> active = getDefaultProfiles(binder, "spring.profiles.active"); |
708 |
| - activeProfiles.addAll(active); |
| 700 | + activeProfiles.addAll(getDefaultProfiles(binder, "spring.profiles.active")); |
709 | 701 | }
|
710 |
| - this.environment.getPropertySources().replace(DEFAULT_PROPERTIES, defaultProperties); |
711 |
| - } |
712 |
| - } |
713 |
| - |
714 |
| - private List<String> getDefaultProfiles(Binder binder, String property) { |
715 |
| - return binder.bind(property, STRING_LIST).orElse(Collections.emptyList()); |
716 |
| - } |
717 |
| - |
718 |
| - } |
719 |
| - |
720 |
| - private static class FilteredDefaultPropertySource extends MapPropertySource { |
721 |
| - |
722 |
| - private static final List<String> FILTERED_PROPERTY = Arrays.asList("spring.profiles.active", |
723 |
| - "spring.profiles.include"); |
724 |
| - |
725 |
| - FilteredDefaultPropertySource(String name, Map<String, Object> source) { |
726 |
| - super(name, source); |
727 |
| - } |
728 |
| - |
729 |
| - @Override |
730 |
| - public Object getProperty(String name) { |
731 |
| - if (isFilteredProperty(name)) { |
732 |
| - return null; |
733 | 702 | }
|
734 |
| - return super.getProperty(name); |
735 |
| - } |
736 |
| - |
737 |
| - @Override |
738 |
| - public boolean containsProperty(String name) { |
739 |
| - if (isFilteredProperty(name)) { |
740 |
| - return false; |
741 |
| - } |
742 |
| - return super.containsProperty(name); |
| 703 | + this.processedProfiles.stream().filter(this::isDefaultProfile).map(Profile::getName) |
| 704 | + .forEach(activeProfiles::add); |
| 705 | + this.environment.setActiveProfiles(activeProfiles.toArray(new String[0])); |
743 | 706 | }
|
744 | 707 |
|
745 |
| - @Override |
746 |
| - public String[] getPropertyNames() { |
747 |
| - return Arrays.stream(super.getPropertyNames()).filter((name) -> !isFilteredProperty(name)) |
748 |
| - .toArray(String[]::new); |
| 708 | + private boolean isDefaultProfile(Profile profile) { |
| 709 | + return profile != null && !profile.isDefaultProfile(); |
749 | 710 | }
|
750 | 711 |
|
751 |
| - protected boolean isFilteredProperty(String name) { |
752 |
| - return FILTERED_PROPERTY.contains(name); |
| 712 | + private List<String> getDefaultProfiles(Binder binder, String property) { |
| 713 | + return binder.bind(property, STRING_LIST).orElse(Collections.emptyList()); |
753 | 714 | }
|
754 | 715 |
|
755 | 716 | }
|
|
0 commit comments