Browse Source

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
pull/4955/merge
Phillip Webb 10 years ago
parent
commit
ebabc63bbb
  1. 22
      spring-boot/src/main/java/org/springframework/boot/context/ContextIdApplicationContextInitializer.java
  2. 4
      spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java

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

@ -50,12 +50,28 @@ public class ContextIdApplicationContextInitializer implements @@ -50,12 +50,28 @@ public class ContextIdApplicationContextInitializer implements
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}}}}";

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

@ -69,13 +69,13 @@ public class ContextIdApplicationContextInitializerTests { @@ -69,13 +69,13 @@ public class ContextIdApplicationContextInitializerTests {
}
@Test
public void testExplicitName() {
public void testExplicitNameIsChosenInFavorOfCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:spam",
"spring.config.name:foo", "PORT:8080", "vcap.application.name:bar",
"vcap.application.instance_index:2");
this.initializer.initialize(context);
assertEquals("bar:2", context.getId());
assertEquals("spam:2", context.getId());
}
}

Loading…
Cancel
Save