DispatcherServlet 源码分析

init()

  • DispatcherServlet 的父类为 HttpServlet,所以 DispatcherServlet 是一个 Servlet,创建 DispatcherServlet 对象时会执行 init() 方法。
  • init() {
        // 创建springmvc容器,读取配置文件
        // 读取springmvc配置文件时会进行对象的创建,并将创建出来的对象放到springmvc容器中
        WebApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc配置文件.xml");
        // 把容器对象放入到ServletContext中
        getServletContext().setAttribute(key, ctx);
    }

  • 创建 DispatcherServlet 对象时,执行的 init() 方法在 DispatcherServlet 的父类 FrameworkServlet 的父类 HttpServletBean 中
    public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware {
    	public final void init() throws ServletException {
            ......
        	// HttpServletBean 的子类 FrameworkServlet 对该方法有重写,
            // DispatcherServlet 没有对该方法进行重写
            // 所以调用的为 FrameworkServlet 的 initServletBean()
            this.initServletBean(); 
        }
    
        protected void initServletBean() throws ServletException {
        }
    }
    public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
        protected final void initServletBean() throws ServletException {
    
            ......
    
            try {
                // 创建springmvc容器
                this.webApplicationContext = this.initWebApplicationContext();
                this.initFrameworkServlet();
            } catch (RuntimeException | ServletException var4) {
                this.logger.error("Context initialization failed", var4);
                throw var4;
            }
        
            ......
        
        }
    
        // 创建springmvc容器
        protected WebApplicationContext initWebApplicationContext() {
            WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
            WebApplicationContext wac = null; // springmvc容器对象变量
            // springmvc容器是否已经存在
            if (this.webApplicationContext != null) {
                wac = this.webApplicationContext;
                ...
            }
        	
            if (wac == null) {
                // 找springmvc容器
                wac = this.findWebApplicationContext();
            }
    
            if (wac == null) {
                // 找不到springmvc容器就创建springmvc容器
                // 创建springmvc容器过程中会读取springmvc的配置文件
                wac = this.createWebApplicationContext(rootContext);
            }
    
            ...
    
            if (this.publishContext) {
                String attrName = this.getServletContextAttributeName();
                // 将springmvc容器放到web应用级作用域中(本web项目的全局作用域)
                this.getServletContext().setAttribute(attrName, wac);
            }
    
            // 返回springmvc容器
            return wac;
        }
    }

    doDispath()

  • DispatcherServlet 对象是一个 Servlet,当用户发送请求并且该请求后面将会交给 DispatcherServlet 对象处理,tomcate 会调用 DispatcherServlet 对象的 service 方法接收处理请求
  • 在 DispatcherServlet 中没有重写 service 方法,在 HttpServletBean 中也没有,service 方法在 FrameworkServlet 中有进行重写,所以会调用 FrameworkServlet 中的 service 方法
  • public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
    	......
        
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
            if (httpMethod != HttpMethod.PATCH && httpMethod != null) {
                super.service(request, response);
            } else {
                // 执行该方法,处理请求
                this.processRequest(request, response);
            }
    
        }
    
        protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ...
    
            try {
                // 执行该方法
                this.doService(request, response);
            } catch (IOException | ServletException var16) {
                ...
            } catch (Throwable var17) {
                ...
            } finally {
                ...
            }
    
        }
    
        // 由于 FrameworkServlet 中的 doService 方法为抽象方法
        // 所以会调用子类 DispatcherServlet 中重写实现的 doService 方法
        protected abstract void doService(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
        
        ......
    }
    public class DispatcherServlet extends FrameworkServlet {
        ......
        
    	protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
            ...
    
            try {
                // 调用执行该方法
                this.doDispatch(request, response);
            } finally {
                ...
            }
    
        }
    
        // DispatcherServlet 处理请求的核心方法
        protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
            HttpServletRequest processedRequest = request;
            HandlerExecutionChain mappedHandler = null;
            boolean multipartRequestParsed = false;
            WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    
            try {
                try {
                    // 模型和视图
                    ModelAndView mv = null;
                    ...
                    
                        // 会根据请求调用相应的处理器 Controller 对象中相应的用于处理该请求的方法
                        /*
                        如果用户发送 test.do 请求
                        @RequestMapping({"/test.do"})
                        public ModelAndView doSome() {
                            ModelAndView modelAndView = new ModelAndView();
                            modelAndView.addObject("msg", "hello world");
                            modelAndView.setViewName("/show.jsp");
                            return modelAndView;
                        }
                        */
                    	// mv 接收返回的 ModelAndView 对象
                        mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                    
                    ...
                } catch (Exception var22) {
                    this.triggerAfterCompletion(processedRequest, response, mappedHandler, var22);
                } catch (Throwable var23) {
                    this.triggerAfterCompletion(processedRequest, response, mappedHandler, new NestedServletException("Handler processing failed", var23));
                }
    
            } finally {
                ...
            }
        }
    
        ......
    }

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值