spring-MVC源码,请求追踪,运行流程(最新,实用,简单,爆炸!!!!!)

第二就是源码希望大家耐心看到第二

第一:初识spring mvc,熟悉spring mvc运行流程

图中数字,表示请求顺序:

《spring-MVC源码,请求追踪,运行流程(最新,实用,简单,爆炸!!!!!)》

第二:spring mvc 核心-DispatcherServler源码解析

1.DispatcherServlet 的父类 HttpServletBean 覆盖了 HttpServlet 的 init 方法,实现该 servlet 的初始化。

DispatcheServlet还有一个父类 FrameworkServlet(其下的 initServletBean,复写了HttpServletBean的initServletBean

(加一些理论知识:每一个DispatcherServlet,都会有一个一个自己的web应用上下文(WebApplicationContext),也就是ioc容器,它继承了在web应用交集(Contexted)初始化定义的所有bean,这些被继承的bean在具体的servlet中作用域也被重新定义-作用域问题请查看spring篇bean的作用域)总结,所以spring MVC是基于spring IOC的

注:所以在web.xml直接引入DispatcherServlet即可

/**
     * Map config parameters onto bean properties of this servlet, and
     * invoke subclass initialization.
     * @throws ServletException if bean properties are invalid (or required
     * properties are missing), or if subclass initialization fails.
     */
    @Override
    public final void init() throws ServletException {
        if (logger.isDebugEnabled()) {
            logger.debug("Initializing servlet '" + getServletName() + "'");
        }

        // Set bean properties from init parameters.
        try {
            PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
            BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
            ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
            bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
            initBeanWrapper(bw);
            bw.setPropertyValues(pvs, true);
        }
        catch (BeansException ex) {
            logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
            throw ex;
        }

        // Let subclasses do whatever initialization they like.
        initServletBean();

        if (logger.isDebugEnabled()) {
            logger.debug("Servlet '" + getServletName() + "' configured successfully");
        }
    }

2.init上的注释说:initServletBean交给由其子类实现

initServletBean上的注释说:让他们做任何他们喜欢做的初始化

实际上,他会调用他的父类FrameworkServlet的这个方法,实际上这个方法是改写了HttpServertBean的initServletBean()上面域有说道(可以返回去看)。

protected final void initServletBean() throws ServletException, BeansException {  
        getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");  
        if (this.logger.isInfoEnabled()) {  
            this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");  
        }  
        long startTime = System.currentTimeMillis();  
        try {  
            this.webApplicationContext = initWebApplicationContext();  
            initFrameworkServlet();  
        }  
        catch (ServletException ex) {  
            this.logger.error("Context initialization failed", ex);  
            throw ex;  
        }  
        catch (BeansException ex) {  
            this.logger.error("Context initialization failed", ex);  
            throw ex;  
        }  
        if (this.logger.isInfoEnabled()) {  
            long elapsedTime = System.currentTimeMillis() - startTime;  
            this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +  
                    elapsedTime + " ms");  
        }  
    } 

值得注意的一点,此方法是final,不能被重写。

在这代码里就能看到,其中有调用initWebApplicationContext(),这方法再往下追究意思就是,继获IOCwe上下文。

总体就先讲这些~~谢谢大家关注奥



    原文作者:Spring MVC
    原文地址: https://blog.csdn.net/weixin_37352094/article/details/80424193
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞