Skip to content

Commit 8ce7da9

Browse files
committed
Avoid duplicate customization of management web server factory
Previously, customization was performed in two places: 1. By customizers defined in the reactive and servlet web servlet factory auto-configuration - ServletWebServerFactoryAutoConfiguration - ReactiveWebServerFactoryAutoConfiguration 2. By a ManagementWebServerFactoryCustomizer that delegates to customizers of certain types found in the application context hierarchy. This led to some double customization as the customizers registered by the auto-configuration classes were also found and called by the ManagementWebServerFactoryCustomizer. Additionally, the ManagementWebServerFactoryCustomizer would find customizers from the parent context registered by EmbeddedWebServerFactoryCustomizerAutoConfiguration. This commit reworks the customization of the management web server factory to remove the double customization. ManagementWebServerFactoryCustomizer no longer delegates to customizers that it finds in the context hierarchy. This prevents the customizers defined in the reactive and servlet web server factory auto-configuration classes from being called twice. Additionally, EmbeddedWebServerFactoryCustomizerAutoConfiguration is now registered in the child context so that its customizers continue to be called when preparing the management context web server factory. Closes gh-44151
1 parent 40059a0 commit 8ce7da9

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementChildContextConfiguration.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,15 +26,7 @@
2626
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementWebServerFactoryCustomizer;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
29-
import org.springframework.boot.autoconfigure.web.embedded.JettyVirtualThreadsWebServerFactoryCustomizer;
30-
import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer;
31-
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
32-
import org.springframework.boot.autoconfigure.web.embedded.TomcatVirtualThreadsWebServerFactoryCustomizer;
33-
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
34-
import org.springframework.boot.autoconfigure.web.embedded.UndertowWebServerFactoryCustomizer;
35-
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer;
36-
import org.springframework.boot.autoconfigure.web.reactive.TomcatReactiveWebServerFactoryCustomizer;
37-
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
29+
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
3830
import org.springframework.context.ApplicationContext;
3931
import org.springframework.context.annotation.Bean;
4032
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
@@ -58,9 +50,9 @@
5850
public class ReactiveManagementChildContextConfiguration {
5951

6052
@Bean
61-
public ReactiveManagementWebServerFactoryCustomizer reactiveManagementWebServerFactoryCustomizer(
53+
public ManagementWebServerFactoryCustomizer<ConfigurableWebServerFactory> reactiveManagementWebServerFactoryCustomizer(
6254
ListableBeanFactory beanFactory) {
63-
return new ReactiveManagementWebServerFactoryCustomizer(beanFactory);
55+
return new ManagementWebServerFactoryCustomizer<>(beanFactory);
6456
}
6557

6658
@Bean
@@ -73,17 +65,4 @@ public HttpHandler httpHandler(ApplicationContext applicationContext, Management
7365
return httpHandler;
7466
}
7567

76-
static class ReactiveManagementWebServerFactoryCustomizer
77-
extends ManagementWebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> {
78-
79-
ReactiveManagementWebServerFactoryCustomizer(ListableBeanFactory beanFactory) {
80-
super(beanFactory, ReactiveWebServerFactoryCustomizer.class, TomcatWebServerFactoryCustomizer.class,
81-
TomcatReactiveWebServerFactoryCustomizer.class,
82-
TomcatVirtualThreadsWebServerFactoryCustomizer.class, JettyWebServerFactoryCustomizer.class,
83-
JettyVirtualThreadsWebServerFactoryCustomizer.class, UndertowWebServerFactoryCustomizer.class,
84-
NettyWebServerFactoryCustomizer.class);
85-
}
86-
87-
}
88-
8968
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
28+
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
2829
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryAutoConfiguration;
2930
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
3031
import org.springframework.context.annotation.Bean;
@@ -44,7 +45,8 @@ public class ReactiveManagementContextAutoConfiguration {
4445
@Bean
4546
public static ManagementContextFactory reactiveWebChildContextFactory() {
4647
return new ManagementContextFactory(WebApplicationType.REACTIVE, ReactiveWebServerFactory.class,
47-
ReactiveWebServerFactoryAutoConfiguration.class);
48+
ReactiveWebServerFactoryAutoConfiguration.class,
49+
EmbeddedWebServerFactoryCustomizerAutoConfiguration.class);
4850
}
4951

5052
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementWebServerFactoryCustomizer.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@
3939
* @author Andy Wilkinson
4040
* @since 2.0.0
4141
*/
42-
public abstract class ManagementWebServerFactoryCustomizer<T extends ConfigurableWebServerFactory>
42+
public class ManagementWebServerFactoryCustomizer<T extends ConfigurableWebServerFactory>
4343
implements WebServerFactoryCustomizer<T>, Ordered {
4444

4545
private final ListableBeanFactory beanFactory;
@@ -48,12 +48,24 @@ public abstract class ManagementWebServerFactoryCustomizer<T extends Configurabl
4848

4949
@SafeVarargs
5050
@SuppressWarnings("varargs")
51+
@Deprecated(since = "3.5.0", forRemoval = true)
5152
protected ManagementWebServerFactoryCustomizer(ListableBeanFactory beanFactory,
5253
Class<? extends WebServerFactoryCustomizer<?>>... customizerClasses) {
5354
this.beanFactory = beanFactory;
5455
this.customizerClasses = customizerClasses;
5556
}
5657

58+
/**
59+
* Creates a new customizer that will retrieve beans using the given
60+
* {@code beanFactory}.
61+
* @param beanFactory the bean factory to use
62+
* @since 3.5.0
63+
*/
64+
public ManagementWebServerFactoryCustomizer(ListableBeanFactory beanFactory) {
65+
this.beanFactory = beanFactory;
66+
this.customizerClasses = null;
67+
}
68+
5769
@Override
5870
public int getOrder() {
5971
return 0;
@@ -65,7 +77,9 @@ public final void customize(T factory) {
6577
.beanOfTypeIncludingAncestors(this.beanFactory, ManagementServerProperties.class);
6678
// Customize as per the parent context first (so e.g. the access logs go to
6779
// the same place)
68-
customizeSameAsParentContext(factory);
80+
if (this.customizerClasses != null) {
81+
customizeSameAsParentContext(factory);
82+
}
6983
// Then reset the error pages
7084
factory.setErrorPages(Collections.emptySet());
7185
// and add the management-specific bits

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@
3939
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
4040
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
4141
import org.springframework.boot.autoconfigure.web.ServerProperties;
42-
import org.springframework.boot.autoconfigure.web.embedded.JettyVirtualThreadsWebServerFactoryCustomizer;
43-
import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer;
44-
import org.springframework.boot.autoconfigure.web.embedded.TomcatVirtualThreadsWebServerFactoryCustomizer;
45-
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
46-
import org.springframework.boot.autoconfigure.web.embedded.UndertowWebServerFactoryCustomizer;
47-
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
48-
import org.springframework.boot.autoconfigure.web.servlet.TomcatServletWebServerFactoryCustomizer;
49-
import org.springframework.boot.autoconfigure.web.servlet.UndertowServletWebServerFactoryCustomizer;
5042
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5143
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
5244
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
@@ -125,10 +117,7 @@ static class ServletManagementWebServerFactoryCustomizer
125117
extends ManagementWebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
126118

127119
ServletManagementWebServerFactoryCustomizer(ListableBeanFactory beanFactory) {
128-
super(beanFactory, ServletWebServerFactoryCustomizer.class, TomcatServletWebServerFactoryCustomizer.class,
129-
TomcatWebServerFactoryCustomizer.class, TomcatVirtualThreadsWebServerFactoryCustomizer.class,
130-
JettyWebServerFactoryCustomizer.class, JettyVirtualThreadsWebServerFactoryCustomizer.class,
131-
UndertowServletWebServerFactoryCustomizer.class, UndertowWebServerFactoryCustomizer.class);
120+
super(beanFactory);
132121
}
133122

134123
@Override

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementContextAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
30+
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
3031
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
3132
import org.springframework.boot.web.servlet.filter.ApplicationContextHeaderFilter;
3233
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
@@ -49,7 +50,8 @@ public class ServletManagementContextAutoConfiguration {
4950
@Bean
5051
public static ManagementContextFactory servletWebChildContextFactory() {
5152
return new ManagementContextFactory(WebApplicationType.SERVLET, ServletWebServerFactory.class,
52-
ServletWebServerFactoryAutoConfiguration.class);
53+
ServletWebServerFactoryAutoConfiguration.class,
54+
EmbeddedWebServerFactoryCustomizerAutoConfiguration.class);
5355
}
5456

5557
@Bean

0 commit comments

Comments
 (0)