Skip to content

Commit 88f53a3

Browse files
committed
Add prefix support for environment variables
Creates an environmentPrefix property that is used to filter the SystemEnvironmentPropertySource's map of environment variables and create a new one only with the prefixed properties. This new map is used on a new SystemEnvironmentPropertySource that replaces the former one. Fixes spring-projects#3450
1 parent 3664895 commit 88f53a3

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ public class SpringApplication {
177177

178178
private ConfigurableEnvironment environment;
179179

180+
private String environmentPrefix = "";
181+
180182
private Class<? extends ConfigurableApplicationContext> applicationContextClass;
181183

182184
private boolean webEnvironment;
@@ -428,6 +430,11 @@ protected void configureEnvironment(ConfigurableEnvironment environment, String[
428430
protected void configurePropertySources(ConfigurableEnvironment environment,
429431
String[] args) {
430432
MutablePropertySources sources = environment.getPropertySources();
433+
434+
if(this.environmentPrefix != null && !this.environmentPrefix.equals("")) {
435+
filterSystemEnvironmentPropertySourceByPrefix(environment);
436+
}
437+
431438
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
432439
sources.addLast(new MapPropertySource("defaultProperties",
433440
this.defaultProperties));
@@ -448,6 +455,26 @@ protected void configurePropertySources(ConfigurableEnvironment environment,
448455
}
449456
}
450457

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+
451478
/**
452479
* Configure which profiles are active (or active by default) for this application
453480
* environment. Consider overriding this method to programmatically enforce profile
@@ -842,6 +869,15 @@ public void setEnvironment(ConfigurableEnvironment environment) {
842869
this.environment = environment;
843870
}
844871

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+
845881
/**
846882
* Returns a mutable set of the sources that will be added to an ApplicationContext
847883
* when {@link #run(String...)} is called.
@@ -1051,5 +1087,4 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
10511087
Collections.sort(list, AnnotationAwareOrderComparator.INSTANCE);
10521088
return new LinkedHashSet<E>(list);
10531089
}
1054-
10551090
}

0 commit comments

Comments
 (0)