diff --git a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/ConfigurableTomcatWebServerFactory.java b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/ConfigurableTomcatWebServerFactory.java index bd48e764663..4a8bf76f71e 100644 --- a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/ConfigurableTomcatWebServerFactory.java +++ b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/ConfigurableTomcatWebServerFactory.java @@ -91,4 +91,11 @@ public interface ConfigurableTomcatWebServerFactory extends ConfigurableWebServe */ void setUseApr(boolean useApr); + /** + * Set whether the factory should disable Tomcat's MBean registry prior to creating + * the server. + * @param disableMBeanRegistry whether to disable the MBean registry + */ + void setDisableMBeanRegistry(boolean disableMBeanRegistry); + } diff --git a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatWebServerFactory.java b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatWebServerFactory.java index d0bd56f0e37..ecc545c0500 100644 --- a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatWebServerFactory.java +++ b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatWebServerFactory.java @@ -351,6 +351,7 @@ public class TomcatWebServerFactory extends AbstractConfigurableWebServerFactory * the server. * @param disableMBeanRegistry whether to disable the MBean registry */ + @Override public void setDisableMBeanRegistry(boolean disableMBeanRegistry) { this.disableMBeanRegistry = disableMBeanRegistry; } diff --git a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java index a4df3305efd..e97dec446cd 100644 --- a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java @@ -152,6 +152,9 @@ public class TomcatWebServerFactoryCustomizer .as(this::joinCharacters) .whenHasText() .to((relaxedChars) -> customizeRelaxedQueryChars(factory, relaxedChars)); + map.from(this.tomcatProperties.getMbeanregistry()::isEnabled) + .as((enable) -> !enable) + .to(factory::setDisableMBeanRegistry); customizeStaticResources(factory); customizeErrorReportValve(this.serverProperties.getError(), factory); factory.setUseApr(getUseApr(this.tomcatProperties.getUseApr())); diff --git a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerAutoConfiguration.java b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerAutoConfiguration.java index 255c0272de2..2b400f0d85b 100644 --- a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerAutoConfiguration.java +++ b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerAutoConfiguration.java @@ -51,12 +51,6 @@ import org.springframework.http.ReactiveHttpInputMessage; @Import({ TomcatWebServerConfiguration.class, ReactiveWebServerConfiguration.class }) public class TomcatReactiveWebServerAutoConfiguration { - @Bean - TomcatReactiveWebServerFactoryCustomizer tomcatReactiveWebServerFactoryCustomizer( - TomcatServerProperties tomcatProperties) { - return new TomcatReactiveWebServerFactoryCustomizer(tomcatProperties); - } - @Bean @ConditionalOnMissingBean(ReactiveWebServerFactory.class) TomcatReactiveWebServerFactory tomcatReactiveWebServerFactory( diff --git a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerFactoryCustomizer.java deleted file mode 100644 index 886996af5d4..00000000000 --- a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/reactive/TomcatReactiveWebServerFactoryCustomizer.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2012-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.tomcat.autoconfigure.reactive; - -import org.springframework.boot.tomcat.autoconfigure.TomcatServerProperties; -import org.springframework.boot.tomcat.reactive.TomcatReactiveWebServerFactory; -import org.springframework.boot.web.server.WebServerFactoryCustomizer; - -/** - * {@link WebServerFactoryCustomizer} to apply {@link TomcatServerProperties} to Tomcat - * reactive web servers. - * - * @author Andy Wilkinson - * @since 4.0.0 - */ -public class TomcatReactiveWebServerFactoryCustomizer - implements WebServerFactoryCustomizer { - - private final TomcatServerProperties tomcatProperties; - - public TomcatReactiveWebServerFactoryCustomizer(TomcatServerProperties tomcatProperties) { - this.tomcatProperties = tomcatProperties; - } - - @Override - public void customize(TomcatReactiveWebServerFactory factory) { - factory.setDisableMBeanRegistry(!this.tomcatProperties.getMbeanregistry().isEnabled()); - } - -} diff --git a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/servlet/TomcatServletWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/servlet/TomcatServletWebServerFactoryCustomizer.java index eb430307004..a64dcda7003 100644 --- a/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/servlet/TomcatServletWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/servlet/TomcatServletWebServerFactoryCustomizer.java @@ -53,7 +53,6 @@ class TomcatServletWebServerFactoryCustomizer customizeRedirectContextRoot(factory, this.tomcatProperties.getRedirectContextRoot()); } customizeUseRelativeRedirects(factory, this.tomcatProperties.isUseRelativeRedirects()); - factory.setDisableMBeanRegistry(!this.tomcatProperties.getMbeanregistry().isEnabled()); } private void customizeRedirectContextRoot(ConfigurableTomcatWebServerFactory factory, boolean redirectContextRoot) { diff --git a/spring-boot-project/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java index 3f0970e5b1e..ece60e281b4 100644 --- a/spring-boot-project/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java @@ -588,6 +588,15 @@ class TomcatWebServerFactoryCustomizerTests { }); } + @Test + void enableMBeanRegistry() { + bind("server.tomcat.mbeanregistry.enabled=true"); + TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0); + assertThat(factory.isDisableMBeanRegistry()).isTrue(); + this.customizer.customize(factory); + assertThat(factory.isDisableMBeanRegistry()).isFalse(); + } + private void bind(String... inlinedProperties) { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, inlinedProperties); Binder binder = new Binder(ConfigurationPropertySources.get(this.environment));