Skip to content

Commit ed91ad8

Browse files
committed
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 spring-projectsgh-214
1 parent 72bc4c1 commit ed91ad8

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ private ConfigurableApplicationContext bootstrapServiceContext(
138138
.resolvePlaceholders("${spring.cloud.bootstrap.location:}");
139139
Map<String, Object> bootstrapMap = new HashMap<>();
140140
bootstrapMap.put("spring.config.name", configName);
141-
bootstrapMap.put("spring.application.name", configName);
142141
if (StringUtils.hasText(configLocation)) {
143142
bootstrapMap.put("spring.config.location", configLocation);
144143
}
@@ -176,6 +175,10 @@ private ConfigurableApplicationContext bootstrapServiceContext(
176175
AnnotationAwareOrderComparator.sort(sources);
177176
builder.sources(sources.toArray(new Class[sources.size()]));
178177
final ConfigurableApplicationContext context = builder.run();
178+
// gh-214 using spring.application.name=bootstrap to set the context id via
179+
// `ContextIdApplicationContextInitializer` prevents apps from getting the actual spring.application.name
180+
// during the bootstrap phase.
181+
context.setId("bootstrap");
179182
// Make the bootstrap context a parent of the app context
180183
addAncestorInitializer(application, context);
181184
// It only has properties in it now that we don't want in the parent so remove
@@ -192,12 +195,6 @@ private void mergeDefaultProperties(MutablePropertySources environment,
192195
return;
193196
}
194197
PropertySource<?> source = bootstrap.get(name);
195-
if (source instanceof MapPropertySource) {
196-
Map<String, Object> map = ((MapPropertySource) source).getSource();
197-
// The application name is "bootstrap" (by default) at this point and
198-
// we don't want that to appear in the parent context at all.
199-
map.remove("spring.application.name");
200-
}
201198
if (!environment.contains(name)) {
202199
environment.addLast(source);
203200
}

0 commit comments

Comments
 (0)