@@ -177,6 +177,8 @@ public class SpringApplication {
177
177
178
178
private ConfigurableEnvironment environment ;
179
179
180
+ private String environmentPrefix = "" ;
181
+
180
182
private Class <? extends ConfigurableApplicationContext > applicationContextClass ;
181
183
182
184
private boolean webEnvironment ;
@@ -428,6 +430,11 @@ protected void configureEnvironment(ConfigurableEnvironment environment, String[
428
430
protected void configurePropertySources (ConfigurableEnvironment environment ,
429
431
String [] args ) {
430
432
MutablePropertySources sources = environment .getPropertySources ();
433
+
434
+ if (this .environmentPrefix != null && !this .environmentPrefix .equals ("" )) {
435
+ filterSystemEnvironmentPropertySourceByPrefix (environment );
436
+ }
437
+
431
438
if (this .defaultProperties != null && !this .defaultProperties .isEmpty ()) {
432
439
sources .addLast (new MapPropertySource ("defaultProperties" ,
433
440
this .defaultProperties ));
@@ -448,6 +455,26 @@ protected void configurePropertySources(ConfigurableEnvironment environment,
448
455
}
449
456
}
450
457
458
+ private void filterSystemEnvironmentPropertySourceByPrefix (
459
+ ConfigurableEnvironment environment ) {
460
+ final Map <String , Object > systemEnvironment = environment .getSystemEnvironment ();
461
+ final Map <String , Object > prefixedSystemEnvironment = new HashMap <String , Object >(
462
+ systemEnvironment .size ());
463
+
464
+ for (String key : systemEnvironment .keySet ()) {
465
+ if (key .startsWith (environmentPrefix )) {
466
+ prefixedSystemEnvironment .put (key .substring (environmentPrefix .length ()),
467
+ systemEnvironment .get (key ));
468
+ }
469
+ }
470
+
471
+ environment .getPropertySources ()
472
+ .replace (StandardEnvironment .SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME ,
473
+ new org .springframework .core .env .SystemEnvironmentPropertySource (
474
+ StandardEnvironment .SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME ,
475
+ prefixedSystemEnvironment ));
476
+ }
477
+
451
478
/**
452
479
* Configure which profiles are active (or active by default) for this application
453
480
* environment. Consider overriding this method to programmatically enforce profile
@@ -842,6 +869,15 @@ public void setEnvironment(ConfigurableEnvironment environment) {
842
869
this .environment = environment ;
843
870
}
844
871
872
+ /**
873
+ * Sets the environment variables prefix that will be used to filter those variables
874
+ * for this application.
875
+ * @param environmentPrefix the prefix
876
+ */
877
+ public void setEnvironmentPrefix (String environmentPrefix ) {
878
+ this .environmentPrefix = environmentPrefix .toUpperCase ();
879
+ }
880
+
845
881
/**
846
882
* Returns a mutable set of the sources that will be added to an ApplicationContext
847
883
* when {@link #run(String...)} is called.
@@ -1051,5 +1087,4 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
1051
1087
Collections .sort (list , AnnotationAwareOrderComparator .INSTANCE );
1052
1088
return new LinkedHashSet <E >(list );
1053
1089
}
1054
-
1055
1090
}
0 commit comments