Skip to content

Commit 7af6665

Browse files
committed
Ignore management.server.port for war
Fixes gh-14148
1 parent 39d3830 commit 7af6665

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
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+
1922
import org.springframework.beans.factory.SmartInitializingSingleton;
2023
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
2124
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
@@ -26,6 +29,7 @@
2629
import org.springframework.boot.context.event.ApplicationFailedEvent;
2730
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2831
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
32+
import org.springframework.boot.web.context.WebServerApplicationContext;
2933
import org.springframework.context.ApplicationContext;
3034
import org.springframework.context.ApplicationEvent;
3135
import org.springframework.context.ApplicationListener;
@@ -55,6 +59,9 @@
5559
ManagementServerProperties.class })
5660
public class ManagementContextAutoConfiguration {
5761

62+
private static final Log logger = LogFactory
63+
.getLog(ManagementContextAutoConfiguration.class);
64+
5865
@Configuration
5966
@ConditionalOnManagementPort(ManagementPortType.SAME)
6067
static class SameManagementContextConfiguration
@@ -129,16 +136,25 @@ static class DifferentManagementContextConfiguration
129136

130137
@Override
131138
public void afterSingletonsInstantiated() {
132-
ConfigurableWebServerApplicationContext managementContext = this.managementContextFactory
133-
.createManagementContext(this.applicationContext,
134-
EnableChildManagementContextConfiguration.class,
135-
PropertyPlaceholderAutoConfiguration.class);
136-
managementContext.setServerNamespace("management");
137-
managementContext.setId(this.applicationContext.getId() + ":management");
138-
setClassLoaderIfPossible(managementContext);
139-
CloseManagementContextListener.addIfPossible(this.applicationContext,
140-
managementContext);
141-
managementContext.refresh();
139+
if (this.applicationContext instanceof WebServerApplicationContext
140+
&& ((WebServerApplicationContext) this.applicationContext)
141+
.getWebServer() != null) {
142+
ConfigurableWebServerApplicationContext managementContext = this.managementContextFactory
143+
.createManagementContext(this.applicationContext,
144+
EnableChildManagementContextConfiguration.class,
145+
PropertyPlaceholderAutoConfiguration.class);
146+
managementContext.setServerNamespace("management");
147+
managementContext.setId(this.applicationContext.getId() + ":management");
148+
setClassLoaderIfPossible(managementContext);
149+
CloseManagementContextListener.addIfPossible(this.applicationContext,
150+
managementContext);
151+
managementContext.refresh();
152+
}
153+
else {
154+
logger.warn("Could not start embedded management container on "
155+
+ "different port (management endpoints are still available "
156+
+ "through JMX)");
157+
}
142158
}
143159

144160
private void setClassLoaderIfPossible(ConfigurableApplicationContext child) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.web.server;
17+
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
22+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
23+
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
26+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
27+
import org.springframework.boot.test.rule.OutputCapture;
28+
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link ManagementContextAutoConfiguration}.
34+
*
35+
* @author Madhura Bhave
36+
*/
37+
public class ManagementContextAutoConfigurationTests {
38+
39+
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
40+
.withConfiguration(
41+
AutoConfigurations.of(ManagementContextAutoConfiguration.class,
42+
ServletManagementContextAutoConfiguration.class));
43+
44+
@Rule
45+
public OutputCapture output = new OutputCapture();
46+
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+
58+
@Test
59+
public void childManagementContextShouldStartForEmbeddedServer() {
60+
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(
61+
AnnotationConfigServletWebServerApplicationContext::new)
62+
.withConfiguration(AutoConfigurations.of(
63+
ManagementContextAutoConfiguration.class,
64+
ServletWebServerFactoryAutoConfiguration.class,
65+
ServletManagementContextAutoConfiguration.class,
66+
WebEndpointAutoConfiguration.class,
67+
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)"));
72+
}
73+
74+
}

0 commit comments

Comments
 (0)