diff --git a/spring/webflux/spring webflux.md b/spring/webflux/spring webflux.md index 89354a2..c69cdee 100644 --- a/spring/webflux/spring webflux.md +++ b/spring/webflux/spring webflux.md @@ -320,3 +320,127 @@ webflux应用中包含的spring configuration通常包括: - 被委托给`webHandler`的beans - 其他 +上述配置将被被`WebHttpHandlerBuilder`使用,用于构建process chain,示例如下所示: +```java +ApplicationContext context = ... +HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build(); +``` +上述示例中返回的handler可以和`server adapter`结合使用。 + +### DispatcherHandler委托 +`DispatcherHandler`会将请求的委托给特定的bean对象,bean对象会处理请求并且将结果渲染到response中。 + +DispatcherHandler会对如下类型的bean进行auto-detect。 + + ++++ + + + + + + + + + + + + + + + + + + + + +
Bean typeExplanation

HandlerMapping

Map a request to a handler. The mapping is based on some criteria, the details of + which vary by HandlerMapping implementation — annotated controllers, simple + URL pattern mappings, and others.

+

The main HandlerMapping implementations are RequestMappingHandlerMapping for + @RequestMapping annotated methods, RouterFunctionMapping for functional endpoint + routes, and SimpleUrlHandlerMapping for explicit registrations of URI path patterns + and WebHandler instances.

HandlerAdapter

Help the DispatcherHandler to invoke a handler mapped to a request regardless of + how the handler is actually invoked. For example, invoking an annotated controller + requires resolving annotations. The main purpose of a HandlerAdapter is to shield the + DispatcherHandler from such details.

HandlerResultHandler

Process the result from the handler invocation and finalize the response. + See Result Handling.

+ +### Processing +`DispatcherHandler`按照如下方式处理请求: +- 首先,会挨个查询`HandlerMapping`用于查找匹配的handler,会匹配第一个匹配的handler +- 如果匹配到handler,会通过对应的HandlerAdapter执行该handler,并且会将handler返回的值作为`HandlerResult`暴露 +- `HandlerResult`将会被分配给指定的`HandlerResultHandler`进行处理,可能会直接向response中写入数据或渲染view + +### Result Handling +通过`HandlerAdapter`对handler进行调用,返回的结果会被包裹在`HandlerResult`中,随之还包含额外的上下文。 + +`HandlerResult`将会被传递给第一个支持其的上下文,下表中展示了`HandlerResultHandler`的实现: + + +++++ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Result Handler TypeReturn ValuesDefault Order

ResponseEntityResultHandler

ResponseEntity, typically from @Controller instances.

0

ServerResponseResultHandler

ServerResponse, typically from functional endpoints.

0

ResponseBodyResultHandler

Handle return values from @ResponseBody methods or @RestController classes.

100

ViewResolutionResultHandler

CharSequence, View, + Model, Map, + Rendering, + or any other Object is treated as a model attribute.

+

See also View Resolution.

Integer.MAX_VALUE

+ +## Annotated Controllers +spring webflux提供了基于注解的编程模型,通过使用注解来向外暴露request mappings。通过注解标注的controllers可包含灵活的方法签名,并且无需继承任何基类或实现指定接口。 + +使用示例如下所示: +```java +@RestController +public class HelloController { + + @GetMapping("/hello") + public String handle() { + return "Hello WebFlux"; + } +} +``` + +### @Controller +在基于注解的webflux变成模型中,可以使用`@Controller`或`@RestController`来声明controller bean。 + +#### AOP +对于部分场景,需要在运行时使用aop proxy来对controller进行decorate。 + +对于针对controller的aop,推荐使用`class-based proxy`,可以使用`@EnableTransactionManagement(proxyTargetClass=true)`来实现。 +