在Spring Boot中@RequestMapping内部如何工作? [英] How @RequestMapping internally works in Spring Boot?

查看:190
本文介绍了在Spring Boot中@RequestMapping内部如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@RestController
@RequestMapping("/employee")
public class Employee {
  @RequestMapping("/save")
  public void saveEmployee() {
    // saving employee
  }
}

@RequestMapping如何在内部工作以将请求映射到saveEmployee方法?

How does @RequestMapping will work internally to map the request to the saveEmployee method?

推荐答案

在应用程序启动期间,Spring将通过XML Config,Java Config或组件扫描的方式识别所有Bean并将它们存储在ApplicationContext中.

During application startup, Spring will identify all Beans by way of XML Config, Java Config, or Component Scanning and store them in the ApplicationContext.

Spring Boot为您自动配置了许多Bean,包括

Spring Boot autoconfigures many Beans for you, including RequestMappingHandlerMapping.

当此Bean为

When this Bean is initialized it scans the ApplicationContext for any Beans annotated with @Controller.

然后遍历每个Controller bean和

Then it iterates over each Controller bean and looks for methods annotated with @RequestMapping. Finally it persists these mapping/handler pairs in the MappingRegistry

DispatcherServlet是您应用程序的中央HTTP请求处理程序,它将

The DispatcherServlet is the central HTTP request handler for your application and it will search the ApplicationContext for any Beans that implement the HandlerMapping interface, which the RequestMappingHandlerMapping Bean does (by way of inheritance).

然后遍历这些bean,要求它们为该请求解析相应的处理程序. RequestMappingHandlerMapping bean将通过

Then it iterates over these beans asking them to resolve the corresponding handler for this request. The RequestMappingHandlerMapping bean will resolve the handler by searching its MappingRegistry.

找到匹配项后,处理程序方法最终为

When a match is found, the handler method is eventually invoked.

这篇关于在Spring Boot中@RequestMapping内部如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆