Tweak ApplicationContext ID
VCAP environemt applied consistently, and more tests. [Fixes #60750138] [bs-351] Add API for application context id
This commit is contained in:
parent
89332e230e
commit
6d4d495003
|
|
@ -115,11 +115,6 @@
|
||||||
<artifactId>jcl-over-slf4j</artifactId>
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-jdk14</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
||||||
|
|
@ -49,15 +49,22 @@ import org.springframework.util.StringUtils;
|
||||||
public class ContextIdApplicationContextInitializer implements
|
public class ContextIdApplicationContextInitializer implements
|
||||||
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
|
ApplicationContextInitializer<ConfigurableApplicationContext>, 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;
|
private int order = Integer.MAX_VALUE - 10;
|
||||||
|
|
||||||
public ContextIdApplicationContextInitializer() {
|
public ContextIdApplicationContextInitializer() {
|
||||||
this("${spring.application.name:${vcap.application.name:"
|
this(NAME_PATTERN);
|
||||||
+ "${spring.config.name:application}}}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -83,11 +90,8 @@ public class ContextIdApplicationContextInitializer implements
|
||||||
|
|
||||||
private String getApplicationId(ConfigurableEnvironment environment) {
|
private String getApplicationId(ConfigurableEnvironment environment) {
|
||||||
String name = environment.resolvePlaceholders(this.name);
|
String name = environment.resolvePlaceholders(this.name);
|
||||||
int index = environment.getProperty("PORT", Integer.class, this.index);
|
String index = environment.resolvePlaceholders(INDEX_PATTERN);
|
||||||
index = environment.getProperty("vcap.application.instance_index", Integer.class,
|
if (!"null".equals(index)) {
|
||||||
index);
|
|
||||||
index = environment.getProperty("spring.application.index", Integer.class, index);
|
|
||||||
if (index >= 0) {
|
|
||||||
return name + ":" + index;
|
return name + ":" + index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,14 @@ public class SpringApplicationTests {
|
||||||
verify(application).printBanner();
|
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
|
@Test
|
||||||
public void specificApplicationContextClass() throws Exception {
|
public void specificApplicationContextClass() throws Exception {
|
||||||
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
SpringApplication application = new SpringApplication(ExampleConfig.class);
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package org.springframework.boot.context.initializer;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.TestUtils;
|
import org.springframework.boot.TestUtils;
|
||||||
import org.springframework.boot.context.initializer.ContextIdApplicationContextInitializer;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
|
@ -70,11 +69,10 @@ public class ContextIdApplicationContextInitializerTests {
|
||||||
public void testExplicitName() {
|
public void testExplicitName() {
|
||||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
|
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
TestUtils.addEnviroment(context, "spring.application.name:spam",
|
TestUtils.addEnviroment(context, "spring.application.name:spam",
|
||||||
"spring.config.name:foo", "PORT:8080",
|
"spring.config.name:foo", "PORT:8080", "vcap.application.name:bar",
|
||||||
"vcap.application.application_name:bar",
|
|
||||||
"vcap.application.instance_index:2");
|
"vcap.application.instance_index:2");
|
||||||
this.initializer.initialize(context);
|
this.initializer.initialize(context);
|
||||||
assertEquals("spam:2", context.getId());
|
assertEquals("bar:2", context.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<property name="LOG_PATTERN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] spring - ${PID:-????} %5p [%t] --- %c{1}: %m%n"/>
|
||||||
|
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-/tmp/logs/service.log}}"/>
|
||||||
|
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>${LOG_PATTERN}</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<logger name="org.springframework.boot.context.annotation" level="TRACE" />
|
||||||
|
<logger name="org.springframework.boot.context.initializer" level="TRACE" />
|
||||||
|
<logger name="org.springframework.boot.config" level="TRACE" />
|
||||||
|
<logger name="org.thymeleaf" level="TRACE" />
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="CONSOLE" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
Loading…
Reference in New Issue