Skip to content

Commit 8ff78ed

Browse files
SergeyHydrambhave
authored andcommitted
Apply server customizer beans automatically
See gh-16584
1 parent fd14cd0 commit 8ff78ed

File tree

4 files changed

+136
-7
lines changed

4 files changed

+136
-7
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2727
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
28+
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
2829
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
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;
34+
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
35+
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
3336
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
3437
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
3538
import org.springframework.context.annotation.Bean;
@@ -45,6 +48,7 @@
4548
*
4649
* @author Brian Clozel
4750
* @author Raheela Aslam
51+
* @author Sergey Serdyuk
4852
*/
4953
abstract class ReactiveWebServerFactoryConfiguration {
5054

@@ -105,8 +109,11 @@ public JettyResourceFactory jettyServerResourceFactory() {
105109

106110
@Bean
107111
public JettyReactiveWebServerFactory jettyReactiveWebServerFactory(
108-
JettyResourceFactory resourceFactory) {
112+
JettyResourceFactory resourceFactory,
113+
ObjectProvider<JettyServerCustomizer> serverCustomizers) {
109114
JettyReactiveWebServerFactory serverFactory = new JettyReactiveWebServerFactory();
115+
serverFactory.getServerCustomizers().addAll(
116+
serverCustomizers.orderedStream().collect(Collectors.toList()));
110117
serverFactory.setResourceFactory(resourceFactory);
111118
return serverFactory;
112119
}
@@ -119,8 +126,15 @@ public JettyReactiveWebServerFactory jettyReactiveWebServerFactory(
119126
static class EmbeddedUndertow {
120127

121128
@Bean
122-
public UndertowReactiveWebServerFactory undertowReactiveWebServerFactory() {
123-
return new UndertowReactiveWebServerFactory();
129+
public UndertowReactiveWebServerFactory undertowReactiveWebServerFactory(
130+
ObjectProvider<UndertowDeploymentInfoCustomizer> deploymentInfoCustomizers,
131+
ObjectProvider<UndertowBuilderCustomizer> builderCustomizers) {
132+
UndertowReactiveWebServerFactory factory = new UndertowReactiveWebServerFactory();
133+
factory.getDeploymentInfoCustomizers().addAll(deploymentInfoCustomizers
134+
.orderedStream().collect(Collectors.toList()));
135+
factory.getBuilderCustomizers().addAll(
136+
builderCustomizers.orderedStream().collect(Collectors.toList()));
137+
return factory;
124138
}
125139

126140
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3333
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3434
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
35+
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
3536
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
3637
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
3738
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
3839
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
3940
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
41+
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
42+
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
4043
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
4144
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
4245
import org.springframework.context.annotation.Bean;
@@ -54,6 +57,7 @@
5457
* @author Brian Clozel
5558
* @author Stephane Nicoll
5659
* @author Raheela Asalm
60+
* @author Sergey Serdyuk
5761
*/
5862
@Configuration(proxyBeanMethods = false)
5963
class ServletWebServerFactoryConfiguration {
@@ -93,8 +97,12 @@ public TomcatServletWebServerFactory tomcatServletWebServerFactory(
9397
public static class EmbeddedJetty {
9498

9599
@Bean
96-
public JettyServletWebServerFactory JettyServletWebServerFactory() {
97-
return new JettyServletWebServerFactory();
100+
public JettyServletWebServerFactory JettyServletWebServerFactory(
101+
ObjectProvider<JettyServerCustomizer> serverCustomizers) {
102+
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
103+
factory.getServerCustomizers().addAll(
104+
serverCustomizers.orderedStream().collect(Collectors.toList()));
105+
return factory;
98106
}
99107

100108
}
@@ -109,8 +117,15 @@ public JettyServletWebServerFactory JettyServletWebServerFactory() {
109117
public static class EmbeddedUndertow {
110118

111119
@Bean
112-
public UndertowServletWebServerFactory undertowServletWebServerFactory() {
113-
return new UndertowServletWebServerFactory();
120+
public UndertowServletWebServerFactory undertowServletWebServerFactory(
121+
ObjectProvider<UndertowDeploymentInfoCustomizer> deploymentInfoCustomizers,
122+
ObjectProvider<UndertowBuilderCustomizer> builderCustomizers) {
123+
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
124+
factory.getDeploymentInfoCustomizers().addAll(deploymentInfoCustomizers
125+
.orderedStream().collect(Collectors.toList()));
126+
factory.getBuilderCustomizers().addAll(
127+
builderCustomizers.orderedStream().collect(Collectors.toList()));
128+
return factory;
114129
}
115130

116131
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,26 @@
2020
import org.mockito.Mockito;
2121

2222
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
2324
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
25+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
26+
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
27+
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
28+
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
2429
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
2530
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
2631
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
2732
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
33+
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
34+
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
35+
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
36+
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
2837
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
2938
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
3039
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
3140
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
3241
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
42+
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
3343
import org.springframework.context.ApplicationContextException;
3444
import org.springframework.context.annotation.Bean;
3545
import org.springframework.context.annotation.Configuration;
@@ -149,6 +159,48 @@ public void tomcatProtocolHandlerCustomizerBeanIsAddedToFactory() {
149159
});
150160
}
151161

162+
@Test
163+
public void jettyServerCustomizerBeanIsAddedToFactory() {
164+
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
165+
AnnotationConfigReactiveWebApplicationContext::new)
166+
.withConfiguration(AutoConfigurations
167+
.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+
});
174+
}
175+
176+
@Test
177+
public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
178+
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
179+
AnnotationConfigReactiveWebApplicationContext::new)
180+
.withConfiguration(AutoConfigurations
181+
.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+
});
188+
}
189+
190+
@Test
191+
public void undertowBuilderCustomizerBeanIsAddedToFactory() {
192+
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
193+
AnnotationConfigReactiveWebApplicationContext::new)
194+
.withConfiguration(AutoConfigurations
195+
.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+
}
203+
152204
@Test
153205
public void forwardedHeaderTransformerShouldBeConfigured() {
154206
this.contextRunner.withUserConfiguration(HttpHandlerConfiguration.class)

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@
3131
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
3232
import org.springframework.boot.test.context.runner.ContextConsumer;
3333
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
34+
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
35+
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
3436
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
3537
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
3638
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
3739
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
40+
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
41+
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
42+
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServer;
43+
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
3844
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
3945
import org.springframework.boot.web.servlet.FilterRegistrationBean;
4046
import org.springframework.boot.web.servlet.ServletRegistrationBean;
@@ -147,6 +153,48 @@ public void initParametersAreConfiguredOnTheServletContext() {
147153
});
148154
}
149155

156+
@Test
157+
public void jettyServerCustomizerBeanIsAddedToFactory() {
158+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
159+
AnnotationConfigServletWebServerApplicationContext::new)
160+
.withConfiguration(AutoConfigurations
161+
.of(ServletWebServerFactoryAutoConfiguration.class))
162+
.withUserConfiguration(JettyServerCustomizer.class);
163+
runner.run((context) -> {
164+
JettyServletWebServerFactory factory = context
165+
.getBean(JettyServletWebServerFactory.class);
166+
assertThat(factory.getServerCustomizers()).hasSize(1);
167+
});
168+
}
169+
170+
@Test
171+
public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
172+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
173+
AnnotationConfigServletWebServerApplicationContext::new)
174+
.withConfiguration(AutoConfigurations
175+
.of(ServletWebServerFactoryAutoConfiguration.class))
176+
.withUserConfiguration(UndertowDeploymentInfoCustomizer.class);
177+
runner.run((context) -> {
178+
UndertowServletWebServerFactory factory = context
179+
.getBean(UndertowServletWebServerFactory.class);
180+
assertThat(factory.getDeploymentInfoCustomizers()).hasSize(1);
181+
});
182+
}
183+
184+
@Test
185+
public void undertowBuilderCustomizerBeanIsAddedToFactory() {
186+
WebApplicationContextRunner runner = new WebApplicationContextRunner(
187+
AnnotationConfigServletWebServerApplicationContext::new)
188+
.withConfiguration(AutoConfigurations
189+
.of(ServletWebServerFactoryAutoConfiguration.class))
190+
.withUserConfiguration(UndertowBuilderCustomizer.class);
191+
runner.run((context) -> {
192+
UndertowServletWebServerFactory factory = context
193+
.getBean(UndertowServletWebServerFactory.class);
194+
assertThat(factory.getBuilderCustomizers()).hasSize(1);
195+
});
196+
}
197+
150198
@Test
151199
public void tomcatConnectorCustomizerBeanIsAddedToFactory() {
152200
WebApplicationContextRunner runner = new WebApplicationContextRunner(

0 commit comments

Comments
 (0)