官方文档:中文版地址
SpringMVC中的RootWebApplicationContext与ServletWebApplicationContext的关系:
RootWebApplicationContext与ServletWebApplicationContext容器存在父子关系,RootWeb为父容器,ServletWeb为子容器,子容器可以访问父容器,反之则不行,所有父容器一般存放的是Service、Dao等bean,而子容器中存储Controller、ViewResolver等bean。
SpringMVC中的RootWebApplicationContext与ServletWebApplicationContext的加载顺序:
结论:先初始化RootWebApplication在初始化ServletWebApplicationContext容器
过程:
1、Servlet启动顺序是先调用listener的监听方法,再调用servlet的init()方法。
2、Spring中存在一个ContextLoaderListener,这个Listener监听web项目的启动,项目启动时,会先调用ContextLoaderListener中初始化方法,初始化一个rootWebApplicationContext并放到ServletContext中。
3、然后Servlet初始化的时候会调用DispatcherServlet的init()方法(该方法在父类的父类HttpServletBean中),这时会先从ServletContext中拿到之前初始化的根容器,作为当前容器的父容器,然后初始化当前容器。