Browse Source

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.
pull/1533/head
Rossen Stoyanchev 7 years ago
parent
commit
ed10cab9d3
  1. 69
      spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java
  2. 8
      spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ContextPathIntegrationTests.java
  3. 3
      spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractWebSocketIntegrationTests.java

69
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.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler; 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 * Central dispatcher for HTTP request handlers/controllers. Dispatches to
* handlers for processing a web request, providing convenient mapping facilities. * registered handlers for processing a request, providing convenient mapping
* facilities.
* *
* <p>It can use any {@link HandlerMapping} implementation to control the routing of * <p>{@code DispatcherHandler} discovers the delegate components it needs from
* requests to handler objects. HandlerMapping objects can be defined as beans in * Spring configuration. It detects the following in the application context:
* the application context. * <ul>
* <li>{@link HandlerMapping} -- map requests to handler objects
* <li>{@link HandlerAdapter} -- for using any handler interface
* <li>{@link HandlerResultHandler} -- process handler return values
* </ul>
* *
* <p>It can use any {@link HandlerAdapter}; this allows for using any handler interface. * <p>{@code DispatcherHandler} s also designed to be a Spring bean itself and
* HandlerAdapter objects can be added as beans in the application context. * 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.
* *
* <p>It can use any {@link HandlerResultHandler}; this allows to process the result of * <p>A {@code DispatcherHandler} bean declaration is included in
* the request handling. HandlerResultHandler objects can be added as beans in the * {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}
* application context. * configuration.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 5.0 * @since 5.0
* @see WebHttpHandlerBuilder#applicationContext(ApplicationContext)
*/ */
public class DispatcherHandler implements WebHandler, ApplicationContextAware { public class DispatcherHandler implements WebHandler, ApplicationContextAware {
@ -181,39 +189,4 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
throw new IllegalStateException("No HandlerResultHandler for " + handlerResult.getReturnValue()); 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}.
*
* <p>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();
}
} }

8
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.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.config.EnableWebFlux; 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 * Integration tests that demonstrate running multiple applications under
@ -55,8 +55,8 @@ public class ContextPathIntegrationTests {
context2.register(WebApp2Config.class); context2.register(WebApp2Config.class);
context2.refresh(); context2.refresh();
HttpHandler webApp1Handler = DispatcherHandler.toHttpHandler(context1); HttpHandler webApp1Handler = WebHttpHandlerBuilder.applicationContext(context1).build();
HttpHandler webApp2Handler = DispatcherHandler.toHttpHandler(context2); HttpHandler webApp2Handler = WebHttpHandlerBuilder.applicationContext(context2).build();
this.server = new ReactorHttpServer(); this.server = new ReactorHttpServer();

3
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.RxNettyRequestUpgradeStrategy;
import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy; import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy;
import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy; import org.springframework.web.reactive.socket.server.upgrade.UndertowRequestUpgradeStrategy;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import static org.junit.Assume.*; import static org.junit.Assume.*;
@ -160,7 +161,7 @@ public abstract class AbstractWebSocketIntegrationTests {
context.register(DispatcherConfig.class, this.serverConfigClass); context.register(DispatcherConfig.class, this.serverConfigClass);
context.register(getWebConfigClass()); context.register(getWebConfigClass());
context.refresh(); context.refresh();
return DispatcherHandler.toHttpHandler(context); return WebHttpHandlerBuilder.applicationContext(context).build();
} }
protected URI getUrl(String path) throws URISyntaxException { protected URI getUrl(String path) throws URISyntaxException {

Loading…
Cancel
Save