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 16acc25e5fa..43cb94fd412 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 @@ -26,7 +26,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.endpoint.Endpoint; @@ -38,6 +37,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; @@ -50,6 +50,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.servlet.DispatcherServlet; @@ -133,7 +134,8 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, // is intentionally not completely auto-configured. childContext.register(EndpointWebMvcChildContextConfiguration.class, PropertyPlaceholderAutoConfiguration.class, - EmbeddedServletContainerAutoConfiguration.class); + EmbeddedServletContainerAutoConfiguration.class, + DispatcherServletAutoConfiguration.class); // Ensure close on the parent also closes the child if (this.applicationContext instanceof ConfigurableApplicationContext) { @@ -154,7 +156,7 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, DISABLE, SAME, DIFFERENT; - public static ManagementServerPort get(BeanFactory beanFactory) { + public static ManagementServerPort get(ApplicationContext beanFactory) { ServerProperties serverProperties; try { @@ -176,6 +178,10 @@ public class EndpointWebMvcAutoConfiguration implements ApplicationContextAware, if (DISABLED_PORT.equals(managementServerProperties.getPort())) { return DISABLE; } + if (!(beanFactory instanceof GenericWebApplicationContext)) { + // Current context is no a a webapp + return DIFFERENT; + } return managementServerProperties.getPort() == null || serverProperties.getPort() == managementServerProperties.getPort() ? SAME : DIFFERENT; diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcChildContextConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcChildContextConfiguration.java index ef6ccc41a81..474db566545 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcChildContextConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcChildContextConfiguration.java @@ -19,7 +19,9 @@ package org.springframework.boot.actuate.autoconfigure; import javax.servlet.Filter; import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.HierarchicalBeanFactory; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerAdapter; import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping; @@ -44,17 +46,31 @@ import org.springframework.web.servlet.HandlerMapping; * @see EndpointWebMvcAutoConfiguration */ @Configuration -public class EndpointWebMvcChildContextConfiguration implements - EmbeddedServletContainerCustomizer { +public class EndpointWebMvcChildContextConfiguration { - @Autowired - private ManagementServerProperties managementServerProperties; + @Configuration + protected static class ServerCustomization implements + EmbeddedServletContainerCustomizer { + + @Autowired + private ListableBeanFactory beanFactory; + + // This needs to be lazily initialized because EmbeddedServletContainerCustomizer + // instances get their callback very early in the context lifecycle. + private ManagementServerProperties managementServerProperties; + + @Override + public void customize(ConfigurableEmbeddedServletContainerFactory factory) { + if (this.managementServerProperties == null) { + this.managementServerProperties = BeanFactoryUtils + .beanOfTypeIncludingAncestors(this.beanFactory, + ManagementServerProperties.class); + } + factory.setPort(this.managementServerProperties.getPort()); + factory.setAddress(this.managementServerProperties.getAddress()); + factory.setContextPath(this.managementServerProperties.getContextPath()); + } - @Override - public void customize(ConfigurableEmbeddedServletContainerFactory factory) { - factory.setPort(this.managementServerProperties.getPort()); - factory.setAddress(this.managementServerProperties.getAddress()); - factory.setContextPath(this.managementServerProperties.getContextPath()); } @Bean diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java index 2628136857a..408262e8605 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; @@ -41,6 +42,7 @@ import org.springframework.web.servlet.DispatcherServlet; */ @Order(Ordered.HIGHEST_PRECEDENCE) @Configuration +@ConditionalOnWebApplication @ConditionalOnClass(DispatcherServlet.class) @AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class) public class DispatcherServletAutoConfiguration { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java index 72aba05b276..7aef7b0e2f0 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration.java @@ -31,6 +31,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration.EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; @@ -53,6 +54,7 @@ import org.springframework.core.type.AnnotationMetadata; */ @Order(Ordered.HIGHEST_PRECEDENCE) @Configuration +@ConditionalOnWebApplication @Import(EmbeddedServletContainerCustomizerBeanPostProcessorRegistrar.class) public class EmbeddedServletContainerAutoConfiguration { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index 930a1078dab..a96003787ca 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -34,6 +34,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; @@ -67,6 +68,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; * @author Dave Syer */ @Configuration +@ConditionalOnWebApplication @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)