diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java index cdf9777616..46b96e4278 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java @@ -65,6 +65,14 @@ public class ReactiveWebServerFactoryAutoConfiguration { return new ReactiveWebServerFactoryCustomizer(serverProperties); } + + @Bean + @ConditionalOnClass(name = "org.apache.catalina.startup.Tomcat") + public TomcatReactiveWebServerFactoryCustomizer tomcatReactiveWebServerFactoryCustomizer( + ServerProperties serverProperties) { + return new TomcatReactiveWebServerFactoryCustomizer(serverProperties); + } + @Bean @ConditionalOnMissingBean @ConditionalOnProperty(value = "server.forward-headers-strategy", diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java index 7f55a9ff00..d696d52a94 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java @@ -24,7 +24,6 @@ import reactor.netty.http.server.HttpServer; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory; import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer; import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; @@ -99,12 +98,6 @@ abstract class ReactiveWebServerFactoryConfiguration { return factory; } - @Bean - public TomcatReactiveWebServerFactoryCustomizer tomcatReactiveWebServerFactoryCustomizer( - ServerProperties serverProperties) { - return new TomcatReactiveWebServerFactoryCustomizer(serverProperties); - } - } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/TomcatReactiveWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/TomcatReactiveWebServerFactoryCustomizer.java index 7f8ba6f8b7..e78ded8717 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/TomcatReactiveWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/TomcatReactiveWebServerFactoryCustomizer.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.web.reactive; -import org.apache.tomcat.util.modeler.Registry; - import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; @@ -40,9 +38,8 @@ public class TomcatReactiveWebServerFactoryCustomizer @Override public void customize(TomcatReactiveWebServerFactory factory) { - if (!this.serverProperties.getTomcat().getMbeanregistry().isEnabled()) { - Registry.disableRegistry(); - } + factory.setDisableMBeanRegistry( + !this.serverProperties.getTomcat().getMbeanregistry().isEnabled()); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/TomcatServletWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/TomcatServletWebServerFactoryCustomizer.java index 93f488acc0..f4a783e56e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/TomcatServletWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/TomcatServletWebServerFactoryCustomizer.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.web.servlet; -import org.apache.tomcat.util.modeler.Registry; - import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.web.embedded.tomcat.ConfigurableTomcatWebServerFactory; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; @@ -62,9 +60,7 @@ public class TomcatServletWebServerFactoryCustomizer customizeUseRelativeRedirects(factory, tomcatProperties.getUseRelativeRedirects()); } - if (!tomcatProperties.getMbeanregistry().isEnabled()) { - Registry.disableRegistry(); - } + factory.setDisableMBeanRegistry(!tomcatProperties.getMbeanregistry().isEnabled()); } private void customizeRedirectContextRoot(ConfigurableTomcatWebServerFactory factory, diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java index a92170a6e7..cdd7b48ace 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatReactiveWebServerFactory.java @@ -36,6 +36,7 @@ import org.apache.catalina.startup.Tomcat; import org.apache.coyote.AbstractProtocol; import org.apache.coyote.ProtocolHandler; import org.apache.coyote.http2.Http2Protocol; +import org.apache.tomcat.util.modeler.Registry; import org.apache.tomcat.util.scan.StandardJarScanFilter; import org.springframework.boot.util.LambdaSafe; @@ -82,6 +83,8 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac private int backgroundProcessorDelay; + private boolean disableMBeanRegistry = true; + /** * Create a new {@link TomcatServletWebServerFactory} instance. */ @@ -106,6 +109,9 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac @Override public WebServer getWebServer(HttpHandler httpHandler) { + if (this.disableMBeanRegistry) { + Registry.disableRegistry(); + } Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); @@ -413,4 +419,14 @@ public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFac this.protocol = protocol; } + /** + * Set whether the factory should disable Tomcat's MBean registry prior to creating + * the server. + * @param disableMBeanRegistry whether to disable the MBean registry + * @since 2.2.0 + */ + public void setDisableMBeanRegistry(boolean disableMBeanRegistry) { + this.disableMBeanRegistry = disableMBeanRegistry; + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java index a01da93c02..01bc47be1e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatServletWebServerFactory.java @@ -60,6 +60,7 @@ import org.apache.catalina.webresources.StandardRoot; import org.apache.coyote.AbstractProtocol; import org.apache.coyote.ProtocolHandler; import org.apache.coyote.http2.Http2Protocol; +import org.apache.tomcat.util.modeler.Registry; import org.apache.tomcat.util.scan.StandardJarScanFilter; import org.springframework.boot.util.LambdaSafe; @@ -133,6 +134,8 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto private int backgroundProcessorDelay; + private boolean disableMBeanRegistry = true; + /** * Create a new {@link TomcatServletWebServerFactory} instance. */ @@ -167,6 +170,9 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto @Override public WebServer getWebServer(ServletContextInitializer... initializers) { + if (this.disableMBeanRegistry) { + Registry.disableRegistry(); + } Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); @@ -707,6 +713,16 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto this.backgroundProcessorDelay = delay; } + /** + * Set whether the factory should disable Tomcat's MBean registry prior to creating + * the server. + * @param disableMBeanRegistry whether to disable the MBean registry + * @since 2.2.0 + */ + public void setDisableMBeanRegistry(boolean disableMBeanRegistry) { + this.disableMBeanRegistry = disableMBeanRegistry; + } + /** * {@link LifecycleListener} to disable persistence in the {@link StandardManager}. A * {@link LifecycleListener} is used so not to interfere with Tomcat's default manager