diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java index 2d131767f35..252dc5fde13 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java @@ -52,7 +52,6 @@ import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfigurat import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; -import org.springframework.boot.context.embedded.EmbeddedServletContainerException; import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -141,11 +140,17 @@ public class EndpointWebMvcAutoConfiguration managementPort = ManagementServerPort .get(this.applicationContext.getEnvironment(), this.beanFactory); } - if (managementPort == ManagementServerPort.DIFFERENT - && this.applicationContext instanceof EmbeddedWebApplicationContext - && ((EmbeddedWebApplicationContext) this.applicationContext) - .getEmbeddedServletContainer() != null) { - createChildManagementContext(); + if (managementPort == ManagementServerPort.DIFFERENT) { + if (this.applicationContext instanceof EmbeddedWebApplicationContext + && ((EmbeddedWebApplicationContext) this.applicationContext) + .getEmbeddedServletContainer() != null) { + createChildManagementContext(); + } + else { + logger.warn("Could not start embedded management container on " + + "different port (management endpoints are still available " + + "through JMX)"); + } } if (managementPort == ManagementServerPort.SAME && this.applicationContext .getEnvironment() instanceof ConfigurableEnvironment) { @@ -165,24 +170,8 @@ public class EndpointWebMvcAutoConfiguration DispatcherServletAutoConfiguration.class); CloseEventPropagationListener.addIfPossible(this.applicationContext, childContext); - try { - childContext.refresh(); - managementContextResolver().setApplicationContext(childContext); - } - catch (RuntimeException ex) { - // No support currently for deploying a war with management.port=, - // and this is the signature of that happening - if (ex instanceof EmbeddedServletContainerException - || ex.getCause() instanceof EmbeddedServletContainerException) { - logger.warn( - "Could not start embedded management container (management endpoints " - + "are still available through JMX)"); - logger.debug("Embedded management container startup failed", ex); - } - else { - throw ex; - } - } + childContext.refresh(); + managementContextResolver().setApplicationContext(childContext); } /** diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java index 518aa6f9612..e9d718e38aa 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfigurationTests.java @@ -17,6 +17,8 @@ package org.springframework.boot.actuate.autoconfigure; import java.io.FileNotFoundException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.net.SocketException; import java.net.URI; import java.nio.charset.Charset; @@ -30,7 +32,9 @@ import javax.servlet.http.HttpServletResponse; import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMappingCustomizer; @@ -51,6 +55,7 @@ import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainer; +import org.springframework.boot.context.embedded.EmbeddedServletContainerException; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.boot.context.web.ServerPortInfoApplicationContextInitializer; import org.springframework.boot.test.EnvironmentTestUtils; @@ -95,6 +100,9 @@ import static org.mockito.Mockito.mock; */ public class EndpointWebMvcAutoConfigurationTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext(); private static ThreadLocal ports = new ThreadLocal(); @@ -234,6 +242,28 @@ public class EndpointWebMvcAutoConfigurationTests { assertAllClosed(); } + @Test + public void specificPortsViaPropertiesWithClash() throws Exception { + int managementPort = ports.get().management; + ServerSocket serverSocket = new ServerSocket(); + serverSocket.bind(new InetSocketAddress(managementPort)); + try { + EnvironmentTestUtils.addEnvironment(this.applicationContext, + "server.port:" + ports.get().server, + "management.port:" + ports.get().management); + this.applicationContext.register(RootConfig.class, EndpointConfig.class, + BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, + ErrorMvcAutoConfiguration.class); + this.thrown.expect(EmbeddedServletContainerException.class); + this.applicationContext.refresh(); + this.applicationContext.close(); + } + finally { + serverSocket.close(); + assertAllClosed(); + } + } + @Test public void contextPath() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext,