zoukankan      html  css  js  c++  java
  • 简单跟跟spring源码

    web.xml配置一个ContextLoaderListener监听,实现了servletContext类的监听器ServletContextListener 继承了contextLoader类,在监听到servletContext被创建时执行contextInitialized方法

    在这个方法中contextLoaderListener将启动spring容器,初始化bean的工作委派给了他的父类ContextLoader类来执行

     这里设计到servletContext监听的内容,可以参考博文学习https://blog.csdn.net/qq_15204179/article/details/82055448

    内容加载器ContextLoader创建了spring容器,初始化完内容后将WebApplicationContext实现类对象存到servletContext域中,key为WebApplicationContext的全限定名.ROOT

     1、然后ContextLoader调用configureAndRefreshWebApplicationContext方法

      1)、接口ConfigurableWebApplicationContext继承了WebApplicationContext 作为这个方法的参数传入

      2)、在这个方法中调用了ConfigurableWebApplicationContext的抽象实现类AbstractRefreshableConfigApplicationContextsetConfigLocations()方法servletContext中取出名为contextConfigLocation的<param> spring 配置,所以contextConfigLocation的spring配置名是写死的,如下图:

    XmlWebApplicationContext 的超级父类 AbstractRefreshableConfigApplicationContext setConfigLocations 方法加载配置文件位置configLocations

    2、然后调用AbstractRefreshableConfigApplicationContext 抽象父类AbstractApplicationContextrefresh() 方法初始化bean,这里完成了所有bean初始化准备和实例化的工作

     

     

    这里跟下去就会发现他为每一个bean都创建了一个RootBeanDefinition 初始化,梳理对象关系,参数赋值等

    具体创建过程可以参考这篇文章:https://blog.csdn.net/qq_42394457/article/details/88629022

    dispatcherServelt extends frameworkServlet (extends httpServletbean(extends HttpServlet(extends GenericServlet(implements Servlet, ServletConfig, java.io.Serializable)) implements EnvironmentCapable, EnvironmentAware))

    其中设计到servletConnfig 和servletContext的概念

    web容器会根据配置文件中的<initParam>配置自动将这些初始化参数封装到ServletConfig实例中并作为参数传递到servlet 的Init(ServletConfig sc)方法中。启动时,,servlet的init方法会自动被web容器调用。

    web容器会为每个应用分配一个servletContext对象,用来代表当前web应用,这个servletContext实例是被这个应用的所有servlet共享的,而且在创建每个servletConfig时都会封装一个servletContext的引用进去。可以通过servletConnfig .getServletContext()方法获得。

    servlet为我们提供了两个init方法,httpServletBean只需要实现无参方法,并且被final修饰了,他的子类不能再重写该方法,,有参方法在GenericServlet中已经实现了

    private transient (不用被序列化的)ServletConfig config;

    public void init(ServletConfig config) throws ServletException {

    this.config = config;

    this.init();

    }

    ——————————————————————————————————————————————————————————————————————————————————————

    学习方法推荐:使用idea跟源码很方便,建议可以先制造几个错误,看看抛异常的堆栈信息,然后断点调试

  • 相关阅读:
    Coursera 机器学习笔记(八)
    Coursera 机器学习笔记(七)
    Coursera 机器学习笔记(六)
    Coursera 机器学习笔记(五)
    Coursera 机器学习笔记(四)
    埃拉托斯特尼筛法
    Floyd判圈算法
    多数投票算法
    Coursera 机器学习笔记(三)
    Coursera 机器学习笔记(二)
  • 原文地址:https://www.cnblogs.com/tianhaichao/p/12145715.html
Copyright © 2011-2022 走看看