问题
@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 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
.
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