Skip to content

Commit 23cf856

Browse files
committed
Polish "Apply server customizer beans automatically"
Closes gh-16584
1 parent 8ff78ed commit 23cf856

File tree

2 files changed

+123
-30
lines changed

2 files changed

+123
-30
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfigurationTests.java

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,29 @@
1616

1717
package org.springframework.boot.autoconfigure.web.reactive;
1818

19+
import org.apache.catalina.startup.Tomcat;
20+
import org.eclipse.jetty.server.Server;
1921
import org.junit.Test;
2022
import org.mockito.Mockito;
23+
import reactor.netty.http.server.HttpServer;
2124

2225
import org.springframework.boot.autoconfigure.AutoConfigurations;
23-
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
26+
import org.springframework.boot.test.context.FilteredClassLoader;
2427
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
25-
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2628
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
2729
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
28-
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
2930
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
3031
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
3132
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
3233
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
3334
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
3435
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
3536
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
36-
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
3737
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
3838
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
3939
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
4040
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
4141
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
42-
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
4342
import org.springframework.context.ApplicationContextException;
4443
import org.springframework.context.annotation.Bean;
4544
import org.springframework.context.annotation.Configuration;
@@ -161,44 +160,55 @@ public void tomcatProtocolHandlerCustomizerBeanIsAddedToFactory() {
161160

162161
@Test
163162
public void jettyServerCustomizerBeanIsAddedToFactory() {
164-
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
163+
new ReactiveWebApplicationContextRunner(
165164
AnnotationConfigReactiveWebApplicationContext::new)
166165
.withConfiguration(AutoConfigurations
167166
.of(ReactiveWebServerFactoryAutoConfiguration.class))
168-
.withUserConfiguration(JettyServerCustomizer.class);
169-
runner.run((context) -> {
170-
JettyReactiveWebServerFactory factory = context
171-
.getBean(JettyReactiveWebServerFactory.class);
172-
assertThat(factory.getServerCustomizers()).hasSize(1);
173-
});
167+
.withClassLoader(
168+
new FilteredClassLoader(Tomcat.class, HttpServer.class))
169+
.withUserConfiguration(JettyServerCustomizerConfiguration.class,
170+
HttpHandlerConfiguration.class)
171+
.run((context) -> {
172+
JettyReactiveWebServerFactory factory = context
173+
.getBean(JettyReactiveWebServerFactory.class);
174+
assertThat(factory.getServerCustomizers()).hasSize(1);
175+
});
174176
}
175177

176178
@Test
177179
public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
178-
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
180+
new ReactiveWebApplicationContextRunner(
179181
AnnotationConfigReactiveWebApplicationContext::new)
180182
.withConfiguration(AutoConfigurations
181183
.of(ReactiveWebServerFactoryAutoConfiguration.class))
182-
.withUserConfiguration(UndertowDeploymentInfoCustomizer.class);
183-
runner.run((context) -> {
184-
UndertowReactiveWebServerFactory factory = context
185-
.getBean(UndertowReactiveWebServerFactory.class);
186-
assertThat(factory.getDeploymentInfoCustomizers()).hasSize(1);
187-
});
184+
.withClassLoader(new FilteredClassLoader(Tomcat.class,
185+
HttpServer.class, Server.class))
186+
.withUserConfiguration(
187+
UndertowDeploymentInfoCustomizerConfiguration.class,
188+
HttpHandlerConfiguration.class)
189+
.run((context) -> {
190+
UndertowReactiveWebServerFactory factory = context
191+
.getBean(UndertowReactiveWebServerFactory.class);
192+
assertThat(factory.getDeploymentInfoCustomizers()).hasSize(1);
193+
});
188194
}
189195

190196
@Test
191197
public void undertowBuilderCustomizerBeanIsAddedToFactory() {
192-
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
198+
new ReactiveWebApplicationContextRunner(
193199
AnnotationConfigReactiveWebApplicationContext::new)
194200
.withConfiguration(AutoConfigurations
195201
.of(ReactiveWebServerFactoryAutoConfiguration.class))
196-
.withUserConfiguration(UndertowBuilderCustomizer.class);
197-
runner.run((context) -> {
198-
UndertowReactiveWebServerFactory factory = context
199-
.getBean(UndertowReactiveWebServerFactory.class);
200-
assertThat(factory.getBuilderCustomizers()).hasSize(1);
201-
});
202+
.withClassLoader(new FilteredClassLoader(Tomcat.class,
203+
HttpServer.class, Server.class))
204+
.withUserConfiguration(
205+
UndertowBuilderCustomizerConfiguration.class,
206+
HttpHandlerConfiguration.class)
207+
.run((context) -> {
208+
UndertowReactiveWebServerFactory factory = context
209+
.getBean(UndertowReactiveWebServerFactory.class);
210+
assertThat(factory.getBuilderCustomizers()).hasSize(1);
211+
});
202212
}
203213

204214
@Test
@@ -300,6 +310,42 @@ public TomcatProtocolHandlerCustomizer protocolHandlerCustomizer() {
300310

301311
}
302312

313+
@Configuration(proxyBeanMethods = false)
314+
static class JettyServerCustomizerConfiguration {
315+
316+
@Bean
317+
public JettyServerCustomizer protocolHandlerCustomizer() {
318+
return (server) -> {
319+
320+
};
321+
}
322+
323+
}
324+
325+
@Configuration(proxyBeanMethods = false)
326+
static class UndertowBuilderCustomizerConfiguration {
327+
328+
@Bean
329+
public UndertowBuilderCustomizer protocolHandlerCustomizer() {
330+
return (builder) -> {
331+
332+
};
333+
}
334+
335+
}
336+
337+
@Configuration(proxyBeanMethods = false)
338+
static class UndertowDeploymentInfoCustomizerConfiguration {
339+
340+
@Bean
341+
public UndertowDeploymentInfoCustomizer protocolHandlerCustomizer() {
342+
return (deploymentInfo) -> {
343+
344+
};
345+
}
346+
347+
}
348+
303349
@Configuration(proxyBeanMethods = false)
304350
static class ForwardedHeaderTransformerConfiguration {
305351

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.apache.catalina.startup.Tomcat;
26+
import org.eclipse.jetty.server.Server;
2527
import org.junit.Test;
28+
import reactor.netty.http.server.HttpServer;
2629

2730
import org.springframework.beans.BeansException;
2831
import org.springframework.beans.factory.config.BeanPostProcessor;
2932
import org.springframework.boot.autoconfigure.AutoConfigurations;
3033
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
34+
import org.springframework.boot.test.context.FilteredClassLoader;
3135
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
3236
import org.springframework.boot.test.context.runner.ContextConsumer;
3337
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
@@ -39,7 +43,6 @@
3943
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
4044
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
4145
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
42-
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServer;
4346
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
4447
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
4548
import org.springframework.boot.web.servlet.FilterRegistrationBean;
@@ -157,9 +160,11 @@ public void initParametersAreConfiguredOnTheServletContext() {
157160
public void jettyServerCustomizerBeanIsAddedToFactory() {
158161
WebApplicationContextRunner runner = new WebApplicationContextRunner(
159162
AnnotationConfigServletWebServerApplicationContext::new)
163+
.withClassLoader(
164+
new FilteredClassLoader(Tomcat.class, HttpServer.class))
160165
.withConfiguration(AutoConfigurations
161166
.of(ServletWebServerFactoryAutoConfiguration.class))
162-
.withUserConfiguration(JettyServerCustomizer.class);
167+
.withUserConfiguration(JettyServerCustomizerConfiguration.class);
163168
runner.run((context) -> {
164169
JettyServletWebServerFactory factory = context
165170
.getBean(JettyServletWebServerFactory.class);
@@ -171,9 +176,12 @@ public void jettyServerCustomizerBeanIsAddedToFactory() {
171176
public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
172177
WebApplicationContextRunner runner = new WebApplicationContextRunner(
173178
AnnotationConfigServletWebServerApplicationContext::new)
179+
.withClassLoader(new FilteredClassLoader(Tomcat.class,
180+
HttpServer.class, Server.class))
174181
.withConfiguration(AutoConfigurations
175182
.of(ServletWebServerFactoryAutoConfiguration.class))
176-
.withUserConfiguration(UndertowDeploymentInfoCustomizer.class);
183+
.withUserConfiguration(
184+
UndertowDeploymentInfoCustomizerConfiguration.class);
177185
runner.run((context) -> {
178186
UndertowServletWebServerFactory factory = context
179187
.getBean(UndertowServletWebServerFactory.class);
@@ -185,9 +193,12 @@ public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
185193
public void undertowBuilderCustomizerBeanIsAddedToFactory() {
186194
WebApplicationContextRunner runner = new WebApplicationContextRunner(
187195
AnnotationConfigServletWebServerApplicationContext::new)
196+
.withClassLoader(new FilteredClassLoader(Tomcat.class,
197+
HttpServer.class, Server.class))
188198
.withConfiguration(AutoConfigurations
189199
.of(ServletWebServerFactoryAutoConfiguration.class))
190-
.withUserConfiguration(UndertowBuilderCustomizer.class);
200+
.withUserConfiguration(
201+
UndertowBuilderCustomizerConfiguration.class);
191202
runner.run((context) -> {
192203
UndertowServletWebServerFactory factory = context
193204
.getBean(UndertowServletWebServerFactory.class);
@@ -413,6 +424,42 @@ public TomcatProtocolHandlerCustomizer protocolHandlerCustomizer() {
413424

414425
}
415426

427+
@Configuration(proxyBeanMethods = false)
428+
static class JettyServerCustomizerConfiguration {
429+
430+
@Bean
431+
public JettyServerCustomizer protocolHandlerCustomizer() {
432+
return (server) -> {
433+
434+
};
435+
}
436+
437+
}
438+
439+
@Configuration(proxyBeanMethods = false)
440+
static class UndertowBuilderCustomizerConfiguration {
441+
442+
@Bean
443+
public UndertowBuilderCustomizer protocolHandlerCustomizer() {
444+
return (builder) -> {
445+
446+
};
447+
}
448+
449+
}
450+
451+
@Configuration(proxyBeanMethods = false)
452+
static class UndertowDeploymentInfoCustomizerConfiguration {
453+
454+
@Bean
455+
public UndertowDeploymentInfoCustomizer protocolHandlerCustomizer() {
456+
return (deploymentInfo) -> {
457+
458+
};
459+
}
460+
461+
}
462+
416463
@Configuration(proxyBeanMethods = false)
417464
static class ForwardedHeaderFilterConfiguration {
418465

0 commit comments

Comments
 (0)