在Spring Boot中@RequestMapping内部如何工作? [英] How @RequestMapping internally works in Spring Boot?
问题描述
@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 Bean
s by way of XML Config, Java Config, or Component Scanning and store them in the ApplicationContext
.
Spring Boot autoconfigures many Beans for you, including RequestMappingHandlerMapping.
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屋!