From ed91ad845f623f3adb7b806294d3a0c0956e0872 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 16 Jun 2017 13:42:17 -0600 Subject: [PATCH] Programmatically set context id rather than using spring.application.name. This allows other bootstrap application listeners to resolve the spring.application.name during the bootstrap phase, but allows bootstrap to determine if the context is the bootstrap context. fixes gh-214 --- .../cloud/bootstrap/BootstrapApplicationListener.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java index 9cad9ccd..3357fb4b 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java @@ -138,7 +138,6 @@ public class BootstrapApplicationListener .resolvePlaceholders("${spring.cloud.bootstrap.location:}"); Map bootstrapMap = new HashMap<>(); bootstrapMap.put("spring.config.name", configName); - bootstrapMap.put("spring.application.name", configName); if (StringUtils.hasText(configLocation)) { bootstrapMap.put("spring.config.location", configLocation); } @@ -176,6 +175,10 @@ public class BootstrapApplicationListener AnnotationAwareOrderComparator.sort(sources); builder.sources(sources.toArray(new Class[sources.size()])); final ConfigurableApplicationContext context = builder.run(); + // gh-214 using spring.application.name=bootstrap to set the context id via + // `ContextIdApplicationContextInitializer` prevents apps from getting the actual spring.application.name + // during the bootstrap phase. + context.setId("bootstrap"); // Make the bootstrap context a parent of the app context addAncestorInitializer(application, context); // It only has properties in it now that we don't want in the parent so remove @@ -192,12 +195,6 @@ public class BootstrapApplicationListener return; } PropertySource source = bootstrap.get(name); - if (source instanceof MapPropertySource) { - Map map = ((MapPropertySource) source).getSource(); - // The application name is "bootstrap" (by default) at this point and - // we don't want that to appear in the parent context at all. - map.remove("spring.application.name"); - } if (!environment.contains(name)) { environment.addLast(source); }