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 ;
59
60
import org .springframework .core .env .MutablePropertySources ;
60
61
import org .springframework .core .env .Profiles ;
61
62
import org .springframework .core .env .PropertySource ;
@@ -323,6 +324,9 @@ public void load() {
323
324
this .processedProfiles = new LinkedList <>();
324
325
this .activatedProfiles = false ;
325
326
this .loaded = new LinkedHashMap <>();
327
+ MapPropertySource defaultProperties = (MapPropertySource ) this .environment
328
+ .getPropertySources ().get (DEFAULT_PROPERTIES );
329
+ replaceDefaultPropertySourceIfNecessary (defaultProperties );
326
330
initializeProfiles ();
327
331
while (!this .profiles .isEmpty ()) {
328
332
Profile profile = this .profiles .poll ();
@@ -333,10 +337,19 @@ public void load() {
333
337
addToLoaded (MutablePropertySources ::addLast , false ));
334
338
this .processedProfiles .add (profile );
335
339
}
336
- resetEnvironmentProfiles (this .processedProfiles );
337
340
load (null , this ::getNegativeProfileFilter ,
338
341
addToLoaded (MutablePropertySources ::addFirst , true ));
339
342
addLoadedPropertySources ();
343
+ resetEnvironment (defaultProperties );
344
+ }
345
+
346
+ private void replaceDefaultPropertySourceIfNecessary (
347
+ MapPropertySource defaultProperties ) {
348
+ if (defaultProperties != null ) {
349
+ this .environment .getPropertySources ().replace (DEFAULT_PROPERTIES ,
350
+ new FilteredDefaultPropertySource (DEFAULT_PROPERTIES ,
351
+ defaultProperties .getSource ()));
352
+ }
340
353
}
341
354
342
355
/**
@@ -729,6 +742,76 @@ private void addLoadedPropertySource(MutablePropertySources destination,
729
742
}
730
743
}
731
744
745
+ private void resetEnvironment (MapPropertySource defaultProperties ) {
746
+ List <String > activeProfiles = new ArrayList <>();
747
+ handleDefaultPropertySource (defaultProperties , activeProfiles );
748
+ activeProfiles .addAll (this .processedProfiles .stream ()
749
+ .filter ((profile ) -> profile != null && !profile .isDefaultProfile ())
750
+ .map (Profile ::getName ).collect (Collectors .toList ()));
751
+ this .environment .setActiveProfiles (activeProfiles .toArray (new String [0 ]));
752
+ }
753
+
754
+ private void handleDefaultPropertySource (MapPropertySource defaultProperties ,
755
+ List <String > activeProfiles ) {
756
+ if (defaultProperties != null ) {
757
+ Binder binder = new Binder (
758
+ ConfigurationPropertySources .from (defaultProperties ),
759
+ new PropertySourcesPlaceholdersResolver (this .environment ));
760
+ List <String > includes = getDefaultProfiles (binder ,
761
+ "spring.profiles.include" );
762
+ activeProfiles .addAll (includes );
763
+ if (!this .activatedProfiles ) {
764
+ List <String > active = getDefaultProfiles (binder ,
765
+ "spring.profiles.active" );
766
+ activeProfiles .addAll (active );
767
+ }
768
+ this .environment .getPropertySources ().replace (DEFAULT_PROPERTIES ,
769
+ defaultProperties );
770
+ }
771
+ }
772
+
773
+ private List <String > getDefaultProfiles (Binder binder , String property ) {
774
+ return Arrays
775
+ .asList (binder .bind (property , STRING_ARRAY ).orElse (new String [] {}));
776
+ }
777
+
778
+ }
779
+
780
+ private static class FilteredDefaultPropertySource extends MapPropertySource {
781
+
782
+ private static final List <String > FILTERED_PROPERTY = Arrays
783
+ .asList ("spring.profiles.active" , "spring.profiles.include" );
784
+
785
+ FilteredDefaultPropertySource (String name , Map <String , Object > source ) {
786
+ super (name , source );
787
+ }
788
+
789
+ @ Override
790
+ public Object getProperty (String name ) {
791
+ if (isFilteredProperty (name )) {
792
+ return null ;
793
+ }
794
+ return super .getProperty (name );
795
+ }
796
+
797
+ @ Override
798
+ public boolean containsProperty (String name ) {
799
+ if (isFilteredProperty (name )) {
800
+ return false ;
801
+ }
802
+ return super .containsProperty (name );
803
+ }
804
+
805
+ @ Override
806
+ public String [] getPropertyNames () {
807
+ return Arrays .stream (super .getPropertyNames ())
808
+ .filter ((name ) -> !isFilteredProperty (name )).toArray (String []::new );
809
+ }
810
+
811
+ protected boolean isFilteredProperty (String name ) {
812
+ return FILTERED_PROPERTY .contains (name );
813
+ }
814
+
732
815
}
733
816
734
817
/**
0 commit comments