Skip to content

Commit e388ddf

Browse files
committed
WebHttpHandlerBuilder retains ApplicationContext in copy constructor
Issue: SPR-16972 (cherry picked from commit 2a15962)
1 parent f83a01e commit e388ddf

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -79,6 +79,9 @@ public class WebHttpHandlerBuilder {
7979

8080
private final WebHandler webHandler;
8181

82+
@Nullable
83+
private final ApplicationContext applicationContext;
84+
8285
private final List<WebFilter> filters = new ArrayList<>();
8386

8487
private final List<WebExceptionHandler> exceptionHandlers = new ArrayList<>();
@@ -92,22 +95,11 @@ public class WebHttpHandlerBuilder {
9295
@Nullable
9396
private LocaleContextResolver localeContextResolver;
9497

95-
@Nullable
96-
private ApplicationContext applicationContext;
97-
98-
99-
/**
100-
* Private constructor.
101-
*/
102-
private WebHttpHandlerBuilder(WebHandler webHandler) {
103-
Assert.notNull(webHandler, "WebHandler must not be null");
104-
this.webHandler = webHandler;
105-
}
10698

10799
/**
108100
* Private constructor to use when initialized from an ApplicationContext.
109101
*/
110-
private WebHttpHandlerBuilder(WebHandler webHandler, ApplicationContext applicationContext) {
102+
private WebHttpHandlerBuilder(WebHandler webHandler, @Nullable ApplicationContext applicationContext) {
111103
Assert.notNull(webHandler, "WebHandler must not be null");
112104
this.webHandler = webHandler;
113105
this.applicationContext = applicationContext;
@@ -118,6 +110,7 @@ private WebHttpHandlerBuilder(WebHandler webHandler, ApplicationContext applicat
118110
*/
119111
private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) {
120112
this.webHandler = other.webHandler;
113+
this.applicationContext = other.applicationContext;
121114
this.filters.addAll(other.filters);
122115
this.exceptionHandlers.addAll(other.exceptionHandlers);
123116
this.sessionManager = other.sessionManager;
@@ -132,7 +125,7 @@ private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) {
132125
* @return the prepared builder
133126
*/
134127
public static WebHttpHandlerBuilder webHandler(WebHandler webHandler) {
135-
return new WebHttpHandlerBuilder(webHandler);
128+
return new WebHttpHandlerBuilder(webHandler, null);
136129
}
137130

138131
/**
@@ -156,7 +149,6 @@ public static WebHttpHandlerBuilder webHandler(WebHandler webHandler) {
156149
* @return the prepared builder
157150
*/
158151
public static WebHttpHandlerBuilder applicationContext(ApplicationContext context) {
159-
160152
WebHttpHandlerBuilder builder = new WebHttpHandlerBuilder(
161153
context.getBean(WEB_HANDLER_BEAN_NAME, WebHandler.class), context);
162154

@@ -272,10 +264,7 @@ public WebHttpHandlerBuilder localeContextResolver(LocaleContextResolver localeC
272264
* Build the {@link HttpHandler}.
273265
*/
274266
public HttpHandler build() {
275-
276-
WebHandler decorated;
277-
278-
decorated = new FilteringWebHandler(this.webHandler, this.filters);
267+
WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters);
279268
decorated = new ExceptionHandlingWebHandler(decorated, this.exceptionHandlers);
280269

281270
HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated);

spring-web/src/test/java/org/springframework/web/server/adapter/WebHttpHandlerBuilderTests.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -36,10 +36,8 @@
3636
import org.springframework.web.server.WebFilter;
3737
import org.springframework.web.server.WebHandler;
3838

39-
import static java.time.Duration.ofMillis;
40-
import static org.junit.Assert.assertEquals;
41-
import static org.junit.Assert.assertSame;
42-
import static org.junit.Assert.assertTrue;
39+
import static java.time.Duration.*;
40+
import static org.junit.Assert.*;
4341

4442
/**
4543
* Unit tests for {@link WebHttpHandlerBuilder}.
@@ -48,13 +46,12 @@
4846
public class WebHttpHandlerBuilderTests {
4947

5048
@Test // SPR-15074
51-
public void orderedWebFilterBeans() throws Exception {
49+
public void orderedWebFilterBeans() {
5250
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
5351
context.register(OrderedWebFilterBeanConfig.class);
5452
context.refresh();
5553

5654
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
57-
5855
assertTrue(httpHandler instanceof HttpWebHandlerAdapter);
5956
assertSame(context, ((HttpWebHandlerAdapter) httpHandler).getApplicationContext());
6057

@@ -66,13 +63,12 @@ public void orderedWebFilterBeans() throws Exception {
6663
}
6764

6865
@Test // SPR-15074
69-
public void orderedWebExceptionHandlerBeans() throws Exception {
66+
public void orderedWebExceptionHandlerBeans() {
7067
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
7168
context.register(OrderedExceptionHandlerBeanConfig.class);
7269
context.refresh();
7370

7471
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
75-
7672
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
7773
MockServerHttpResponse response = new MockServerHttpResponse();
7874
httpHandler.handle(request, response).block(ofMillis(5000));
@@ -81,20 +77,30 @@ public void orderedWebExceptionHandlerBeans() throws Exception {
8177
}
8278

8379
@Test
84-
public void configWithoutFilters() throws Exception {
80+
public void configWithoutFilters() {
8581
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
8682
context.register(NoFilterConfig.class);
8783
context.refresh();
8884

8985
HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build();
90-
9186
MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
9287
MockServerHttpResponse response = new MockServerHttpResponse();
9388
httpHandler.handle(request, response).block(ofMillis(5000));
9489

9590
assertEquals("handled", response.getBodyAsString().block(ofMillis(5000)));
9691
}
9792

93+
@Test // SPR-16972
94+
public void cloneWithApplicationContext() {
95+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
96+
context.register(NoFilterConfig.class);
97+
context.refresh();
98+
99+
WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.applicationContext(context);
100+
assertSame(context, ((HttpWebHandlerAdapter) builder.build()).getApplicationContext());
101+
assertSame(context, ((HttpWebHandlerAdapter) builder.clone().build()).getApplicationContext());
102+
}
103+
98104

99105
private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
100106
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);

0 commit comments

Comments
 (0)