diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 0c944a69f1..2eefd2e0d5 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -115,11 +115,6 @@ jcl-over-slf4j test - - org.slf4j - slf4j-jdk14 - test - diff --git a/spring-boot/src/main/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializer.java b/spring-boot/src/main/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializer.java index a33b1e2c22..dcdda3fd82 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializer.java @@ -49,15 +49,22 @@ import org.springframework.util.StringUtils; public class ContextIdApplicationContextInitializer implements ApplicationContextInitializer, Ordered { - private String name; + /** + * Placeholder pattern to resolve for application name + */ + private static final String NAME_PATTERN = "${vcap.application.name:${spring.application.name:${spring.config.name:application}}}"; - private int index = -1; + /** + * Placeholder pattern to resolve for application index + */ + private static final String INDEX_PATTERN = "${vcap.application.instance_index:${spring.application.index:${server.port:${PORT:null}}}}"; + + private String name; private int order = Integer.MAX_VALUE - 10; public ContextIdApplicationContextInitializer() { - this("${spring.application.name:${vcap.application.name:" - + "${spring.config.name:application}}}"); + this(NAME_PATTERN); } /** @@ -83,11 +90,8 @@ public class ContextIdApplicationContextInitializer implements private String getApplicationId(ConfigurableEnvironment environment) { String name = environment.resolvePlaceholders(this.name); - int index = environment.getProperty("PORT", Integer.class, this.index); - index = environment.getProperty("vcap.application.instance_index", Integer.class, - index); - index = environment.getProperty("spring.application.index", Integer.class, index); - if (index >= 0) { + String index = environment.resolvePlaceholders(INDEX_PATTERN); + if (!"null".equals(index)) { return name + ":" + index; } diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index dce0a7ba16..5c9a06df24 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -121,6 +121,14 @@ public class SpringApplicationTests { verify(application).printBanner(); } + @Test + public void customId() throws Exception { + SpringApplication application = new SpringApplication(ExampleConfig.class); + application.setWebEnvironment(false); + this.context = application.run("--spring.application.name=foo"); + assertEquals("foo", this.context.getId()); + } + @Test public void specificApplicationContextClass() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class); diff --git a/spring-boot/src/test/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializerTests.java index 89dc5366b5..daf1e3ceba 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/initializer/ContextIdApplicationContextInitializerTests.java @@ -18,7 +18,6 @@ package org.springframework.boot.context.initializer; import org.junit.Test; import org.springframework.boot.TestUtils; -import org.springframework.boot.context.initializer.ContextIdApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -70,11 +69,10 @@ public class ContextIdApplicationContextInitializerTests { public void testExplicitName() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); TestUtils.addEnviroment(context, "spring.application.name:spam", - "spring.config.name:foo", "PORT:8080", - "vcap.application.application_name:bar", + "spring.config.name:foo", "PORT:8080", "vcap.application.name:bar", "vcap.application.instance_index:2"); this.initializer.initialize(context); - assertEquals("spam:2", context.getId()); + assertEquals("bar:2", context.getId()); } } diff --git a/spring-boot/src/test/resources/logback-test.xml b/spring-boot/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..85ea91eb53 --- /dev/null +++ b/spring-boot/src/test/resources/logback-test.xml @@ -0,0 +1,17 @@ + + + + + + + ${LOG_PATTERN} + + + + + + + + + +