Spring 过源码

对spring的印象从DispatcherServlet开始,就从这个类开始看

从HttpServletBean开始,是spring框架自己的。
总的来说,HttpServlerBean 直接继承了java的HttpServlet,作用是将Servlet的配置的参数设置到相应的属性;
FrameworkServlet初始化了WebApplicationContext
DispatcherServlet初始化了自身的9个组件。

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

由FrameworkServlet重写

@Override
    protected final void initServletBean() throws ServletException {

        if (this.logger.isInfoEnabled()) {
            this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
        }
        long startTime = System.currentTimeMillis();

        try {
            this.webApplicationContext = initWebApplicationContext(); //*
            initFrameworkServlet();
        }

    }

进入initWebApplicationContext() (Initialize and publish the WebApplicationContext for this servlet 初始化和发布spring资源文件)

protected WebApplicationContext initWebApplicationContext() {
        WebApplicationContext rootContext =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        WebApplicationContext wac = null;

        if (this.webApplicationContext != null) {
            // A context instance was injected at construction time -> use it
            wac = this.webApplicationContext;
            if (wac instanceof ConfigurableWebApplicationContext) {
                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
                if (!cwac.isActive()) {
                    // The context has not yet been refreshed -> provide services such as
                    // setting the parent context, setting the application context id, etc
                    if (cwac.getParent() == null) {
                        // The context instance was injected without an explicit parent -> set
                        // the root application context (if any; may be null) as the parent
                        cwac.setParent(rootContext);
                    }
                    configureAndRefreshWebApplicationContext(cwac);
                }
            }
        }
        if (wac == null) {
            // No context instance was injected at construction time -> see if one
            // has been registered in the servlet context. If one exists, it is assumed
            // that the parent context (if any) has already been set and that the
            // user has performed any initialization such as setting the context id
            wac = findWebApplicationContext();
        }
        if (wac == null) {
            // No context instance is defined for this servlet -> create a local one
            wac = createWebApplicationContext(rootContext);
        }

        if (!this.refreshEventReceived) {
            // Either the context is not a ConfigurableApplicationContext with refresh
            // support or the context injected at construction time had already been
            // refreshed -> trigger initial onRefresh manually here.
            onRefresh(wac);
        }

        if (this.publishContext) {
            // Publish the context as a servlet context attribute.
            String attrName = getServletContextAttributeName();
            getServletContext().setAttribute(attrName, wac);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
                        "' as ServletContext attribute with name [" + attrName + "]");
            }
        }

        return wac;
    }
  1. 第一步,判断是不是在构造方法中传递webApplicationContext参数,ServletContext.addServlet方式注册Servlet
  2. 第二步,判断是不是通过web.xml配置文件传入webApplicationContext参数
  3. 第三步,在前面两部探索后都没有找到webApplicationContext的情况下自己创建一个。

第一步和第三步都会调用configureAndRefreshWebApplicationContext()

这里写图片描述

进入DispatherServlet.onRefresh(context)

九大组件初始化入口

九大组件分别初始化

  1. MultipartResplver
    可以没有默认值 ,可以为null
    用于上传请求
  2. LocalResolver
    用于国际化
  3. ThemeResolver
    主题
  4. HandlerMapping
    处理器映射器组件
  5. HandlerAdapter
    处理器适配器组件
  6. HandlerExceptionResolvers
    异常处理器
  7. RequestToViewNameTranslator
    ViewResolver根据ViewName查找View,但是有的handler处理完没有设置view,也没有设置viewname,这就是它的任务了
  8. ViewResolvers
    视图解析器
  9. FlashMapManager
    用来管理 FlashMap

进入doDispath()

  1. mappedHandler = getHandler(processedRequest);
    找到对应的handler处理器,和executionChain
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
  1. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
    再根据处理器调用相应的适配器

  2. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    获取ModelAndView对象,这其中包含了处理结果的视图和视图中要使用的数据

  3. render(mv, request, response);
    找到一个合适的ViewResolver对mv对象进行渲染

view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request);
  1. 最后给浏览器构造一个HTTP响应。

暂时这样,后续再细看!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值