From ebabc63bbbe1e5b9634ecae1088a752c6d9cfdd8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 21 Jan 2016 15:31:33 -0800 Subject: [PATCH] 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 --- ...ontextIdApplicationContextInitializer.java | 22 ++++++++++++++++--- ...tIdApplicationContextInitializerTests.java | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/ContextIdApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/ContextIdApplicationContextInitializer.java index 4dabd469ba7..cc40cf14cda 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/ContextIdApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/ContextIdApplicationContextInitializer.java @@ -50,12 +50,28 @@ public class ContextIdApplicationContextInitializer implements ApplicationContextInitializer, Ordered { /** - * Placeholder pattern to resolve for application name. + * Placeholder pattern to resolve for application name. The following order is used to + * find the name: + * + * 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: + * + * 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}}}}"; diff --git a/spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java index d9306e5c150..48df45a08c7 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/ContextIdApplicationContextInitializerTests.java @@ -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()); } }