How @RequestMapping internally works in Spring Boot?

戏子无情 提交于 2019-12-12 14:47:45

问题


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

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


回答1:


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 autoconfigures many Beans for you, including RequestMappingHandlerMapping.

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

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

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).

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.



来源:https://stackoverflow.com/questions/45329692/how-requestmapping-internally-works-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!