Skip to content

Commit ebabc63

Browse files
committed
Refine ContextId NAME_PATTERN
Update the ContextIdApplicationContextInitializer default NAME_PATTERN to favor the developer defined `${spring.application.name}` value over the deployer defined `${vcap.application.name}`. Fixes gh-4926
1 parent fd3e5cf commit ebabc63

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

spring-boot/src/main/java/org/springframework/boot/context/ContextIdApplicationContextInitializer.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,28 @@ public class ContextIdApplicationContextInitializer implements
5050
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
5151

5252
/**
53-
* Placeholder pattern to resolve for application name.
53+
* Placeholder pattern to resolve for application name. The following order is used to
54+
* find the name:
55+
* <ul>
56+
* <li>{@code spring.application.name}</li>
57+
* <li>{@code vcap.application.name}</li>
58+
* <li>{@code spring.config.name}</li>
59+
* </ul>
60+
* This order allows the user defined name to take precedence over the platform
61+
* defined name. If no property is defined {@code 'application'} will be used.
5462
*/
55-
private static final String NAME_PATTERN = "${vcap.application.name:${spring.application.name:${spring.config.name:application}}}";
63+
private static final String NAME_PATTERN = "${spring.application.name:${vcap.application.name:${spring.config.name:application}}}";
5664

5765
/**
58-
* Placeholder pattern to resolve for application index.
66+
* Placeholder pattern to resolve for application index. The following order is used
67+
* to find the name:
68+
* <ul>
69+
* <li>{@code vcap.application.instance_index}</li>
70+
* <li>{@code spring.application.index}</li>
71+
* <li>{@code server.port}</li>
72+
* <li>{@code PORT}</li>
73+
* </ul>
74+
* This order allows favors a platform defined index over any user defined value.
5975
*/
6076
private static final String INDEX_PATTERN = "${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}";
6177

spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public void testCloudFoundry() {
6969
}
7070

7171
@Test
72-
public void testExplicitName() {
72+
public void testExplicitNameIsChosenInFavorOfCloudFoundry() {
7373
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
7474
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:spam",
7575
"spring.config.name:foo", "PORT:8080", "vcap.application.name:bar",
7676
"vcap.application.instance_index:2");
7777
this.initializer.initialize(context);
78-
assertEquals("bar:2", context.getId());
78+
assertEquals("spam:2", context.getId());
7979
}
8080

8181
}

0 commit comments

Comments
 (0)