From 764e34b9db567895a69ad63d6b4f73829647eb82 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 15 Apr 2015 13:47:30 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20start=20child=20context=20for?= =?UTF-8?q?=20actuator=20endpoints=20when=20not=20embedded?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this commit, EndpointWebMvcAutoConfiguration would start a child context if the management port was different to the server port and the application context was a web application context. This caused two problems: If a user built an executable war and configured the management port so that it was different to the server port, their application would run successfully when launched with java -jar, but it would fail when deployed to Tomcat as an attempt would be made to start embedded Tomcat. Secondly, if a user ran a test annotated with @WebAppConfiguration the main embedded Tomcat instance would not be started, but the child context would trigger the creation of a Tomcat instance listening on the configured management port. This is unexpected as @WebIntegrationTest or @IntegrationTest and @WebAppConfiguration should be required to have the test trigger full startup of the application and listen on the configured ports. This commit updates EndpointWebMvcAutoConfiguration so that it will only start a child context when the management port is different to the server port and the EmbeddedWebApplicationContext has an embedded servlet container. This resolves the two problems described above as there will be no embedded servlet container when deployed to a standalone container or when a test is run without @IntegrationTest. Fixes gh-2798 --- .../EndpointWebMvcAutoConfiguration.java | 10 ++++++--- .../EndpointWebMvcAutoConfigurationTests.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) 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 15fd336182b..3859371cbce 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 @@ -66,6 +66,7 @@ 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.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -148,7 +149,9 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, ManagementServerPort managementPort = ManagementServerPort .get(this.applicationContext); if (managementPort == ManagementServerPort.DIFFERENT - && this.applicationContext instanceof WebApplicationContext) { + && this.applicationContext instanceof EmbeddedWebApplicationContext + && ((EmbeddedWebApplicationContext) this.applicationContext) + .getEmbeddedServletContainer() != null) { createChildManagementContext(); } if (managementPort == ManagementServerPort.SAME @@ -375,8 +378,9 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { AnnotationAttributes annotationAttributes = AnnotationAttributes - .fromMap(metadata.getAnnotationAttributes(ConditionalOnEnabledEndpoint.class - .getName())); + .fromMap(metadata + .getAnnotationAttributes(ConditionalOnEnabledEndpoint.class + .getName())); String endpointName = annotationAttributes.getString("value"); boolean enabledByDefault = annotationAttributes .getBoolean("enabledByDefault"); 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 57154f9ef93..c6ad430f12b 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 @@ -21,7 +21,9 @@ import java.net.SocketException; import java.net.URI; import java.nio.charset.Charset; import java.util.List; +import java.util.Vector; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -78,6 +80,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** * Tests for {@link EndpointWebMvcAutoConfiguration}. @@ -150,6 +154,24 @@ public class EndpointWebMvcAutoConfigurationTests { assertAllClosed(); } + @Test + public void onDifferentPortInServletContainer() throws Exception { + this.applicationContext.register(RootConfig.class, EndpointConfig.class, + DifferentPortConfig.class, BaseConfiguration.class, + EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); + ServletContext servletContext = mock(ServletContext.class); + given(servletContext.getInitParameterNames()).willReturn( + new Vector().elements()); + given(servletContext.getAttributeNames()).willReturn( + new Vector().elements()); + this.applicationContext.setServletContext(servletContext); + this.applicationContext.refresh(); + assertContent("/controller", ports.get().management, null); + assertContent("/endpoint", ports.get().management, null); + this.applicationContext.close(); + assertAllClosed(); + } + @Test public void onRandomPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class,