Spring启动执行流程梳理

注:本文梳理启动流程使用的Spring版本:4.0.2.RELEASE
使用spring配置,都需要在web.xml中配置一个spring的监听器和启动参数(context-param),如下:

    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>
            classpath:spring-*.xml
        param-value>
    context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

web容器启动时,下面为spring加载执行步骤:

  1. 执行web.xml中的ContextLoaderListener监听器
  2. 初始化ContextLoaderListener中的contextInitialized方法
    Spring启动执行流程梳理_第1张图片
  3. contextInitialized方法中调用父类(ContextLoader)的initWebApplicationContext方法
    Spring启动执行流程梳理_第2张图片
  4. initWebApplicationContext方法中执行了三个任务
    • 创建WebApplicationContext容器
      Spring启动执行流程梳理_第3张图片
    • 加载context-param中配置的spring配置文件
    • 初始化配置文件中及创建配置文件中的bean
      Spring启动执行流程梳理_第4张图片
      Spring启动执行流程梳理_第5张图片

web容器停止时候会执行ContextLoaderListener的contextDestroyed方法销毁context容器
Spring启动执行流程梳理_第6张图片

你可能感兴趣的:(Spring)