SpringMVC-运行原理和源码解析

基于SpringBoot版本如下:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

运行原理

关键源码解析

SpringBoot的自动化配置关于SpringMVC的配置类是中是WebMvcAutoConfiguration,详情参见spring.factories
处理器和适配器的bean是在WebMvcAutoConfiguration的内部类 EnableWebMvcConfiguration里配置的。

1、获取应用上下文信息

使用ApplicationContextAware#setApplicationContext的方法初始化应用上下文

2、初始化URL映射处理器

使用InitializingBean#afterPropertiesSet初始化获取所有url和处理器的映射

3、DispatcherServlet初始化

当接收到第一个请求时进行初始化,HttpServlet#init--> DispatcherServlet#onRefresh
然后处理请求,执行doDispatch方法

initHandlerMappings

initHandlerAdapters

4、执行doDispatch方法

通过URL获取处理器,再找到处理器适配器,用适配器执行

getHandler

getHandlerAdapter

posted @ 2022-03-09 19:02  我自逍遥  阅读(68)  评论(0)    收藏  举报