From 2a15962d7fef281134a71a67de7020a7e64aba8b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 25 Jun 2018 18:12:12 +0200 Subject: [PATCH] WebHttpHandlerBuilder retains ApplicationContext in copy constructor Issue: SPR-16972 --- .../server/adapter/WebHttpHandlerBuilder.java | 28 ++++++------------- .../adapter/WebHttpHandlerBuilderTests.java | 28 +++++++++++-------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java b/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java index f7764d0cb0..86b7aaaf97 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/WebHttpHandlerBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,6 +79,9 @@ public class WebHttpHandlerBuilder { private final WebHandler webHandler; + @Nullable + private final ApplicationContext applicationContext; + private final List filters = new ArrayList<>(); private final List exceptionHandlers = new ArrayList<>(); @@ -92,22 +95,11 @@ public class WebHttpHandlerBuilder { @Nullable private LocaleContextResolver localeContextResolver; - @Nullable - private ApplicationContext applicationContext; - - - /** - * Private constructor. - */ - private WebHttpHandlerBuilder(WebHandler webHandler) { - Assert.notNull(webHandler, "WebHandler must not be null"); - this.webHandler = webHandler; - } /** * Private constructor to use when initialized from an ApplicationContext. */ - private WebHttpHandlerBuilder(WebHandler webHandler, ApplicationContext applicationContext) { + private WebHttpHandlerBuilder(WebHandler webHandler, @Nullable ApplicationContext applicationContext) { Assert.notNull(webHandler, "WebHandler must not be null"); this.webHandler = webHandler; this.applicationContext = applicationContext; @@ -118,6 +110,7 @@ public class WebHttpHandlerBuilder { */ private WebHttpHandlerBuilder(WebHttpHandlerBuilder other) { this.webHandler = other.webHandler; + this.applicationContext = other.applicationContext; this.filters.addAll(other.filters); this.exceptionHandlers.addAll(other.exceptionHandlers); this.sessionManager = other.sessionManager; @@ -132,7 +125,7 @@ public class WebHttpHandlerBuilder { * @return the prepared builder */ public static WebHttpHandlerBuilder webHandler(WebHandler webHandler) { - return new WebHttpHandlerBuilder(webHandler); + return new WebHttpHandlerBuilder(webHandler, null); } /** @@ -156,7 +149,6 @@ public class WebHttpHandlerBuilder { * @return the prepared builder */ public static WebHttpHandlerBuilder applicationContext(ApplicationContext context) { - WebHttpHandlerBuilder builder = new WebHttpHandlerBuilder( context.getBean(WEB_HANDLER_BEAN_NAME, WebHandler.class), context); @@ -272,13 +264,10 @@ public class WebHttpHandlerBuilder { * Build the {@link HttpHandler}. */ public HttpHandler build() { - - WebHandler decorated; - decorated = new FilteringWebHandler(this.webHandler, this.filters); + WebHandler decorated = new FilteringWebHandler(this.webHandler, this.filters); decorated = new ExceptionHandlingWebHandler(decorated, this.exceptionHandlers); HttpWebHandlerAdapter adapted = new HttpWebHandlerAdapter(decorated); - if (this.sessionManager != null) { adapted.setSessionManager(this.sessionManager); } @@ -291,7 +280,6 @@ public class WebHttpHandlerBuilder { if (this.applicationContext != null) { adapted.setApplicationContext(this.applicationContext); } - adapted.afterPropertiesSet(); return adapted; diff --git a/spring-web/src/test/java/org/springframework/web/server/adapter/WebHttpHandlerBuilderTests.java b/spring-web/src/test/java/org/springframework/web/server/adapter/WebHttpHandlerBuilderTests.java index 9a4ef1e92a..31eb78d4d3 100644 --- a/spring-web/src/test/java/org/springframework/web/server/adapter/WebHttpHandlerBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/server/adapter/WebHttpHandlerBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,10 +36,8 @@ import org.springframework.web.server.WebExceptionHandler; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebHandler; -import static java.time.Duration.ofMillis; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static java.time.Duration.*; +import static org.junit.Assert.*; /** * Unit tests for {@link WebHttpHandlerBuilder}. @@ -48,13 +46,12 @@ import static org.junit.Assert.assertTrue; public class WebHttpHandlerBuilderTests { @Test // SPR-15074 - public void orderedWebFilterBeans() throws Exception { + public void orderedWebFilterBeans() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(OrderedWebFilterBeanConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); - assertTrue(httpHandler instanceof HttpWebHandlerAdapter); assertSame(context, ((HttpWebHandlerAdapter) httpHandler).getApplicationContext()); @@ -66,13 +63,12 @@ public class WebHttpHandlerBuilderTests { } @Test // SPR-15074 - public void orderedWebExceptionHandlerBeans() throws Exception { + public void orderedWebExceptionHandlerBeans() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(OrderedExceptionHandlerBeanConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); - MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); @@ -81,13 +77,12 @@ public class WebHttpHandlerBuilderTests { } @Test - public void configWithoutFilters() throws Exception { + public void configWithoutFilters() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(NoFilterConfig.class); context.refresh(); HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(context).build(); - MockServerHttpRequest request = MockServerHttpRequest.get("/").build(); MockServerHttpResponse response = new MockServerHttpResponse(); httpHandler.handle(request, response).block(ofMillis(5000)); @@ -95,6 +90,17 @@ public class WebHttpHandlerBuilderTests { assertEquals("handled", response.getBodyAsString().block(ofMillis(5000))); } + @Test // SPR-16972 + public void cloneWithApplicationContext() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(NoFilterConfig.class); + context.refresh(); + + WebHttpHandlerBuilder builder = WebHttpHandlerBuilder.applicationContext(context); + assertSame(context, ((HttpWebHandlerAdapter) builder.build()).getApplicationContext()); + assertSame(context, ((HttpWebHandlerAdapter) builder.clone().build()).getApplicationContext()); + } + private static Mono writeToResponse(ServerWebExchange exchange, String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8);