Skip to content

Commit ff1e507

Browse files
committed
Merge branch '2.1.x'
Closes spring-projectsgh-16473
2 parents 8626a33 + c0a9147 commit ff1e507

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java

+4-17
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.web.server;
1818

19-
import org.apache.commons.logging.Log;
20-
import org.apache.commons.logging.LogFactory;
21-
2219
import org.springframework.beans.factory.SmartInitializingSingleton;
2320
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2421
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
@@ -29,7 +26,7 @@
2926
import org.springframework.boot.context.event.ApplicationFailedEvent;
3027
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3128
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
32-
import org.springframework.boot.web.context.WebServerApplicationContext;
29+
import org.springframework.boot.web.context.WebServerInitializedEvent;
3330
import org.springframework.context.ApplicationContext;
3431
import org.springframework.context.ApplicationEvent;
3532
import org.springframework.context.ApplicationListener;
@@ -59,9 +56,6 @@
5956
ManagementServerProperties.class })
6057
public class ManagementContextAutoConfiguration {
6158

62-
private static final Log logger = LogFactory
63-
.getLog(ManagementContextAutoConfiguration.class);
64-
6559
@Configuration(proxyBeanMethods = false)
6660
@ConditionalOnManagementPort(ManagementPortType.SAME)
6761
static class SameManagementContextConfiguration
@@ -122,7 +116,7 @@ static class EnableSameManagementContextConfiguration {
122116
@Configuration(proxyBeanMethods = false)
123117
@ConditionalOnManagementPort(ManagementPortType.DIFFERENT)
124118
static class DifferentManagementContextConfiguration
125-
implements SmartInitializingSingleton {
119+
implements ApplicationListener<WebServerInitializedEvent> {
126120

127121
private final ApplicationContext applicationContext;
128122

@@ -135,10 +129,8 @@ static class DifferentManagementContextConfiguration
135129
}
136130

137131
@Override
138-
public void afterSingletonsInstantiated() {
139-
if (this.applicationContext instanceof WebServerApplicationContext
140-
&& ((WebServerApplicationContext) this.applicationContext)
141-
.getWebServer() != null) {
132+
public void onApplicationEvent(WebServerInitializedEvent event) {
133+
if (event.getApplicationContext().equals(this.applicationContext)) {
142134
ConfigurableWebServerApplicationContext managementContext = this.managementContextFactory
143135
.createManagementContext(this.applicationContext,
144136
EnableChildManagementContextConfiguration.class,
@@ -150,11 +142,6 @@ public void afterSingletonsInstantiated() {
150142
managementContext);
151143
managementContext.refresh();
152144
}
153-
else {
154-
logger.warn("Could not start embedded management container on "
155-
+ "different port (management endpoints are still available "
156-
+ "through JMX)");
157-
}
158145
}
159146

160147
private void setClassLoaderIfPossible(ConfigurableApplicationContext child) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java

+18-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.boot.actuate.autoconfigure.web.server;
1717

18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
20+
1821
import org.junit.Rule;
1922
import org.junit.Test;
2023

@@ -33,28 +36,13 @@
3336
* Tests for {@link ManagementContextAutoConfiguration}.
3437
*
3538
* @author Madhura Bhave
39+
* @author Andy Wilkinson
3640
*/
3741
public class ManagementContextAutoConfigurationTests {
3842

39-
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
40-
.withConfiguration(
41-
AutoConfigurations.of(ManagementContextAutoConfiguration.class,
42-
ServletManagementContextAutoConfiguration.class));
43-
4443
@Rule
4544
public OutputCapture output = new OutputCapture();
4645

47-
@Test
48-
public void managementServerPortShouldBeIgnoredForNonEmbeddedServer() {
49-
this.contextRunner.withPropertyValues("management.server.port=8081")
50-
.run((context) -> {
51-
assertThat(context.getStartupFailure()).isNull();
52-
assertThat(this.output.toString())
53-
.contains("Could not start embedded management container on "
54-
+ "different port (management endpoints are still available through JMX)");
55-
});
56-
}
57-
5846
@Test
5947
public void childManagementContextShouldStartForEmbeddedServer() {
6048
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
@@ -65,10 +53,19 @@ public void childManagementContextShouldStartForEmbeddedServer() {
6553
ServletManagementContextAutoConfiguration.class,
6654
WebEndpointAutoConfiguration.class,
6755
EndpointAutoConfiguration.class));
68-
contextRunner.withPropertyValues("management.server.port=8081")
69-
.run((context) -> assertThat(this.output.toString()).doesNotContain(
70-
"Could not start embedded management container on "
71-
+ "different port (management endpoints are still available through JMX)"));
56+
contextRunner.withPropertyValues("server.port=0", "management.server.port=0").run(
57+
(context) -> assertThat(tomcatStartedOccurencesIn(this.output.toString()))
58+
.isEqualTo(2));
59+
60+
}
61+
62+
private int tomcatStartedOccurencesIn(String output) {
63+
int matches = 0;
64+
Matcher matcher = Pattern.compile("Tomcat started on port").matcher(output);
65+
while (matcher.find()) {
66+
matches++;
67+
}
68+
return matches;
7269
}
7370

7471
}

0 commit comments

Comments
 (0)