Skip to content

Commit 31a0500

Browse files
committed
Move server-specific knowledge out of general child context creation
Issue: 44068
1 parent 38c5818 commit 31a0500

17 files changed

+728
-205
lines changed
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.
@@ -19,22 +19,11 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22-
import org.springframework.beans.factory.ListableBeanFactory;
2322
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
2423
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
2524
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
26-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementWebServerFactoryCustomizer;
2725
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2826
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;
3827
import org.springframework.context.ApplicationContext;
3928
import org.springframework.context.annotation.Bean;
4029
import org.springframework.http.server.reactive.ContextPathCompositeHandler;
@@ -57,12 +46,6 @@
5746
@ConditionalOnWebApplication(type = Type.REACTIVE)
5847
public class ReactiveManagementChildContextConfiguration {
5948

60-
@Bean
61-
public ReactiveManagementWebServerFactoryCustomizer reactiveManagementWebServerFactoryCustomizer(
62-
ListableBeanFactory beanFactory) {
63-
return new ReactiveManagementWebServerFactoryCustomizer(beanFactory);
64-
}
65-
6649
@Bean
6750
public HttpHandler httpHandler(ApplicationContext applicationContext, ManagementServerProperties properties) {
6851
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build();
@@ -73,17 +56,4 @@ public HttpHandler httpHandler(ApplicationContext applicationContext, Management
7356
return httpHandler;
7457
}
7558

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-
8959
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.reactive;
18+
19+
import org.springframework.beans.factory.ListableBeanFactory;
20+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementWebServerFactoryCustomizer;
21+
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
22+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
23+
24+
/**
25+
* {@link ManagementWebServerFactoryCustomizer} for a reactive web server.
26+
*
27+
* @author Andy Wilkinson
28+
* @since 4.0.0
29+
*/
30+
public class ReactiveManagementWebServerFactoryCustomizer
31+
extends ManagementWebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> {
32+
33+
@SafeVarargs
34+
@SuppressWarnings("varargs")
35+
public ReactiveManagementWebServerFactoryCustomizer(ListableBeanFactory beanFactory,
36+
Class<? extends WebServerFactoryCustomizer<?>>... customizerClasses) {
37+
super(beanFactory, customizerClasses);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.server;
18+
19+
import org.springframework.boot.web.server.WebServerFactory;
20+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
21+
import org.springframework.core.Ordered;
22+
23+
/**
24+
* Base class for a {@link WebServerFactoryCustomizer} that customizes the web server's
25+
* access log.
26+
*
27+
* @param <T> the {@link WebServerFactory} type that can be customized
28+
* @author Andy Wilkinson
29+
* @since 4.0.0
30+
*/
31+
public abstract class AccessLogCustomizer<T extends WebServerFactory>
32+
implements WebServerFactoryCustomizer<T>, Ordered {
33+
34+
private final ManagementServerProperties properties;
35+
36+
protected AccessLogCustomizer(ManagementServerProperties properties) {
37+
this.properties = properties;
38+
}
39+
40+
protected String customizePrefix(String prefix) {
41+
prefix = (prefix != null) ? prefix : "";
42+
if (prefix.startsWith(this.properties.getAccesslog().getPrefix())) {
43+
return prefix;
44+
}
45+
return this.properties.getAccesslog().getPrefix() + prefix;
46+
}
47+
48+
@Override
49+
public int getOrder() {
50+
return 1;
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.server.jetty;
18+
19+
import org.eclipse.jetty.server.Server;
20+
21+
import org.springframework.beans.factory.ListableBeanFactory;
22+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
23+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
24+
import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementWebServerFactoryCustomizer;
25+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
28+
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.reactive.ReactiveWebServerFactoryCustomizer;
32+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
33+
import org.springframework.context.annotation.Bean;
34+
35+
/**
36+
* {@link ManagementContextConfiguration @ManagementContextConfiguration} for Jetty-based
37+
* reactive web endpoint infrastructure when a separate management context running on a
38+
* different port is required.
39+
*
40+
* @author Andy Wilkinson
41+
*/
42+
@ConditionalOnClass(Server.class)
43+
@ConditionalOnWebApplication(type = Type.REACTIVE)
44+
@EnableConfigurationProperties(ManagementServerProperties.class)
45+
@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false)
46+
class JettyReactiveManagementChildContextConfiguration {
47+
48+
@Bean
49+
ReactiveManagementWebServerFactoryCustomizer reactiveManagementWebServerFactoryCustomizer(
50+
ListableBeanFactory beanFactory) {
51+
return new ReactiveManagementWebServerFactoryCustomizer(beanFactory, ReactiveWebServerFactoryCustomizer.class,
52+
JettyWebServerFactoryCustomizer.class, JettyVirtualThreadsWebServerFactoryCustomizer.class);
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.server.jetty;
18+
19+
import java.io.File;
20+
21+
import org.eclipse.jetty.server.CustomRequestLog;
22+
import org.eclipse.jetty.server.RequestLog;
23+
import org.eclipse.jetty.server.RequestLogWriter;
24+
import org.eclipse.jetty.server.Server;
25+
26+
import org.springframework.beans.factory.ListableBeanFactory;
27+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
28+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
29+
import org.springframework.boot.actuate.autoconfigure.web.server.AccessLogCustomizer;
30+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
31+
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementWebServerFactoryCustomizer;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
35+
import org.springframework.boot.autoconfigure.web.embedded.JettyVirtualThreadsWebServerFactoryCustomizer;
36+
import org.springframework.boot.autoconfigure.web.embedded.JettyWebServerFactoryCustomizer;
37+
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
38+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
39+
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
40+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
41+
import org.springframework.context.annotation.Bean;
42+
import org.springframework.util.StringUtils;
43+
44+
/**
45+
* {@link ManagementContextConfiguration @ManagementContextConfiguration} for Jetty-based
46+
* servlet web endpoint infrastructure when a separate management context running on a
47+
* different port is required.
48+
*
49+
* @author Andy Wilkinson
50+
*/
51+
@ConditionalOnClass(Server.class)
52+
@ConditionalOnWebApplication(type = Type.SERVLET)
53+
@EnableConfigurationProperties(ManagementServerProperties.class)
54+
@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false)
55+
class JettyServletManagementChildContextConfiguration {
56+
57+
@Bean
58+
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer(ManagementServerProperties properties) {
59+
return new JettyAccessLogCustomizer(properties);
60+
}
61+
62+
@Bean
63+
ServletManagementWebServerFactoryCustomizer servletManagementWebServerFactoryCustomizer(
64+
ListableBeanFactory beanFactory) {
65+
return new ServletManagementWebServerFactoryCustomizer(beanFactory, ServletWebServerFactoryCustomizer.class,
66+
JettyWebServerFactoryCustomizer.class, JettyVirtualThreadsWebServerFactoryCustomizer.class);
67+
}
68+
69+
static class JettyAccessLogCustomizer extends AccessLogCustomizer<JettyServletWebServerFactory>
70+
implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {
71+
72+
JettyAccessLogCustomizer(ManagementServerProperties properties) {
73+
super(properties);
74+
}
75+
76+
@Override
77+
public void customize(JettyServletWebServerFactory factory) {
78+
factory.addServerCustomizers(this::customizeServer);
79+
}
80+
81+
private void customizeServer(Server server) {
82+
RequestLog requestLog = server.getRequestLog();
83+
if (requestLog instanceof CustomRequestLog customRequestLog) {
84+
customizeRequestLog(customRequestLog);
85+
}
86+
}
87+
88+
private void customizeRequestLog(CustomRequestLog requestLog) {
89+
if (requestLog.getWriter() instanceof RequestLogWriter requestLogWriter) {
90+
customizeRequestLogWriter(requestLogWriter);
91+
}
92+
}
93+
94+
private void customizeRequestLogWriter(RequestLogWriter writer) {
95+
String filename = writer.getFileName();
96+
if (StringUtils.hasLength(filename)) {
97+
File file = new File(filename);
98+
file = new File(file.getParentFile(), customizePrefix(file.getName()));
99+
writer.setFilename(file.getPath());
100+
}
101+
}
102+
103+
}
104+
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Actuator Jetty web server support.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.web.server.jetty;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.web.server.netty;
18+
19+
import reactor.netty.ReactorNetty;
20+
21+
import org.springframework.beans.factory.ListableBeanFactory;
22+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
23+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
24+
import org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementWebServerFactoryCustomizer;
25+
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
28+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
29+
import org.springframework.boot.autoconfigure.web.embedded.NettyWebServerFactoryCustomizer;
30+
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFactoryCustomizer;
31+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
32+
import org.springframework.context.annotation.Bean;
33+
34+
/**
35+
* {@link ManagementContextConfiguration @ManagementContextConfiguration} for Netty-based
36+
* reactive web endpoint infrastructure when a separate management context running on a
37+
* different port is required.
38+
*
39+
* @author Andy Wilkinson
40+
*/
41+
@ConditionalOnClass(ReactorNetty.class)
42+
@ConditionalOnWebApplication(type = Type.REACTIVE)
43+
@EnableConfigurationProperties(ManagementServerProperties.class)
44+
@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false)
45+
class NettyReactiveManagementChildContextConfiguration {
46+
47+
@Bean
48+
ReactiveManagementWebServerFactoryCustomizer reactiveManagementWebServerFactoryCustomizer(
49+
ListableBeanFactory beanFactory) {
50+
return new ReactiveManagementWebServerFactoryCustomizer(beanFactory, ReactiveWebServerFactoryCustomizer.class,
51+
NettyWebServerFactoryCustomizer.class);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)