Commit ebabc63b authored by Phillip Webb's avatar Phillip Webb

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
parent fd3e5cf3
...@@ -50,12 +50,28 @@ public class ContextIdApplicationContextInitializer implements ...@@ -50,12 +50,28 @@ public class ContextIdApplicationContextInitializer implements
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered { ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
/** /**
* Placeholder pattern to resolve for application name. * Placeholder pattern to resolve for application name. The following order is used to
* find the name:
* <ul>
* <li>{@code spring.application.name}</li>
* <li>{@code vcap.application.name}</li>
* <li>{@code spring.config.name}</li>
* </ul>
* This order allows the user defined name to take precedence over the platform
* defined name. If no property is defined {@code 'application'} will be used.
*/ */
private static final String NAME_PATTERN = "${vcap.application.name:${spring.application.name:${spring.config.name:application}}}"; private static final String NAME_PATTERN = "${spring.application.name:${vcap.application.name:${spring.config.name:application}}}";
/** /**
* Placeholder pattern to resolve for application index. * Placeholder pattern to resolve for application index. The following order is used
* to find the name:
* <ul>
* <li>{@code vcap.application.instance_index}</li>
* <li>{@code spring.application.index}</li>
* <li>{@code server.port}</li>
* <li>{@code PORT}</li>
* </ul>
* This order allows favors a platform defined index over any user defined value.
*/ */
private static final String INDEX_PATTERN = "${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}"; private static final String INDEX_PATTERN = "${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}";
......
...@@ -69,13 +69,13 @@ public class ContextIdApplicationContextInitializerTests { ...@@ -69,13 +69,13 @@ public class ContextIdApplicationContextInitializerTests {
} }
@Test @Test
public void testExplicitName() { public void testExplicitNameIsChosenInFavorOfCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:spam", EnvironmentTestUtils.addEnvironment(context, "spring.application.name:spam",
"spring.config.name:foo", "PORT:8080", "vcap.application.name:bar", "spring.config.name:foo", "PORT:8080", "vcap.application.name:bar",
"vcap.application.instance_index:2"); "vcap.application.instance_index:2");
this.initializer.initialize(context); this.initializer.initialize(context);
assertEquals("bar:2", context.getId()); assertEquals("spam:2", context.getId());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment