Spring ApplicationContext启动方式


1.ContextLoaderListener

1.1.ContextLoaderListener源码

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
...
}

ServletContextListener源码:

public interface ServletContextListener extends EventListener {

    /**
    web容器启动后,调用
    **/
    public default void contextInitialized(ServletContextEvent sce) {
    }
   /**
    web容器关闭时调用,web将要关闭,还未关闭
    **/
    public default void contextDestroyed(ServletContextEvent sce) {
    }
}

ContextLoader: 用于spring 容器初始化类

public class ContextLoader {
  ...
}

1.2.流程

2.springBoot方式

2.1.流程

2.2.ServletWebServerApplicationContext

springboot在执行SpringAppliction.run方法时,创建的ApplicationContext是ServletWebServerApplicationContext,

改context实现了 AbstractApplicationContext.onRefresh方法,在onRefresh方法中创建了相应的web容器。