From ed10cab9d3248e3c77ccaa047d9707cf032d0c7f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 22 Sep 2017 16:24:39 -0400 Subject: [PATCH] Remove DispatcherHandler static factory methods The typical way to load DispatcherHandler is to use WebHttpHandlerBuilder#applicationContext which also detecs filters, exception handlers, as well as other beans that are injected into every ServerWebExchange -- custom session manager, localecontext resolver, codecs for form data, multipart data, etc WebHttpHandlerBuilder is the preferred and way so removing the ones on DispatcherHandler. They could always be added back later. --- .../web/reactive/DispatcherHandler.java | 69 ++++++------------- .../ContextPathIntegrationTests.java | 8 +-- .../AbstractWebSocketIntegrationTests.java | 3 +- 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java index c47afea163..ad976e3b2f 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java @@ -31,34 +31,42 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.http.HttpStatus; -import org.springframework.http.server.reactive.HttpHandler; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.lang.Nullable; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebHandler; -import org.springframework.web.server.adapter.HttpWebHandlerAdapter; -import org.springframework.web.server.adapter.WebHttpHandlerBuilder; /** - * Central dispatcher for HTTP request handlers/controllers. Dispatches to registered - * handlers for processing a web request, providing convenient mapping facilities. + * Central dispatcher for HTTP request handlers/controllers. Dispatches to + * registered handlers for processing a request, providing convenient mapping + * facilities. * - *

It can use any {@link HandlerMapping} implementation to control the routing of - * requests to handler objects. HandlerMapping objects can be defined as beans in - * the application context. + *

{@code DispatcherHandler} discovers the delegate components it needs from + * Spring configuration. It detects the following in the application context: + *

* - *

It can use any {@link HandlerAdapter}; this allows for using any handler interface. - * HandlerAdapter objects can be added as beans in the application context. + *

{@code DispatcherHandler} s also designed to be a Spring bean itself and + * implements {@link ApplicationContextAware} for access to the context it runs + * in. If {@code DispatcherHandler} is declared with the bean name "webHandler" + * it is discovered by {@link WebHttpHandlerBuilder#applicationContext} which + * creates a processing chain together with {@code WebFilter}, + * {@code WebExceptionHandler} and others. * - *

It can use any {@link HandlerResultHandler}; this allows to process the result of - * the request handling. HandlerResultHandler objects can be added as beans in the - * application context. + *

A {@code DispatcherHandler} bean declaration is included in + * {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux} + * configuration. * * @author Rossen Stoyanchev * @author Sebastien Deleuze * @author Juergen Hoeller * @since 5.0 + * @see WebHttpHandlerBuilder#applicationContext(ApplicationContext) */ public class DispatcherHandler implements WebHandler, ApplicationContextAware { @@ -181,39 +189,4 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware { throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue()); } - - /** - * Expose a dispatcher-based {@link WebHandler} for the given application context, - * typically for further configuration with filters and exception handlers through - * a {@link org.springframework.web.server.adapter.WebHttpHandlerBuilder}. - * @param applicationContext the application context to find the handler beans in - * @see #DispatcherHandler(ApplicationContext) - * @see org.springframework.web.server.adapter.WebHttpHandlerBuilder#webHandler - */ - public static WebHandler toWebHandler(ApplicationContext applicationContext) { - return new DispatcherHandler(applicationContext); - } - - /** - * Expose a dispatcher-based {@link HttpHandler} for the given application context, - * typically for direct registration with an engine adapter such as - * {@link org.springframework.http.server.reactive.ServletHttpHandlerAdapter}. - * - *

Delegates to {@link WebHttpHandlerBuilder#applicationContext} that - * detects the target {@link DispatcherHandler} along with - * {@link org.springframework.web.server.WebFilter}s, and - * {@link org.springframework.web.server.WebExceptionHandler}s in the given - * ApplicationContext. - * - * @param context the application context to find the handler beans in - * @see #DispatcherHandler(ApplicationContext) - * @see HttpWebHandlerAdapter - * @see org.springframework.http.server.reactive.ServletHttpHandlerAdapter - * @see org.springframework.http.server.reactive.ReactorHttpHandlerAdapter - * @see org.springframework.http.server.reactive.UndertowHttpHandlerAdapter - */ - public static HttpHandler toHttpHandler(ApplicationContext context) { - return WebHttpHandlerBuilder.applicationContext(context).build(); - } - } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java index 50349f48cf..532a30b521 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java @@ -28,10 +28,10 @@ import org.springframework.http.server.reactive.bootstrap.ReactorHttpServer; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; -import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.reactive.config.EnableWebFlux; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * Integration tests that demonstrate running multiple applications under @@ -55,8 +55,8 @@ public class ContextPathIntegrationTests { context2.register(WebApp2Config.class); context2.refresh(); - HttpHandler webApp1Handler = DispatcherHandler.toHttpHandler(context1); - HttpHandler webApp2Handler = DispatcherHandler.toHttpHandler(context2); + HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build(); + HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build(); this.server = new ReactorHttpServer(); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java index 10fd9baa66..3afe7d6def 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java @@ -61,6 +61,7 @@ import org.springframework.web.reactive.socket.server.upgrade.ReactorNettyReques import org.springframework.web.reactive.socket.server.upgrade.RxNettyRequestUpgradeStrategy; import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy; import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import static org.junit.Assume.*; @@ -160,7 +161,7 @@ public abstract class AbstractWebSocketIntegrationTests { context.register(DispatcherConfig.class, this.serverConfigClass); context.register(getWebConfigClass()); context.refresh(); - return DispatcherHandler.toHttpHandler(context); + return WebHttpHandlerBuilder.applicationContext(context).build(); } protected URI getUrl(String path) throws URISyntaxException {