加载spring容器和springmvc子容器的大致过程
1、启动tomcat等web容器,加载web.xml。
2、解析读取web.xml里的< context-param > 上下文参数,即spring容器,spring上下文的参数,通过< param-name > < param-value > 即key-value来指定spring容器的配置文件的位置。
key:contextConfigLocation
value:applicationContext-*.xml
- applicationContext-dao.xml dao 相关的配置
- applicationContext-service.xml service相关的配置
- applicationContext-trans.xml 事务相关的配置
- applicationContext-activemq.xml 比如整合了activemq的相关配置
- applicationContext-redis 比如整合redis缓存的相关配置
- 等等
3、创建Listener ContextLoaderListener利用context-param加载spring的配置并创建spring容器。
ContextLoaderListener实现了ServletContextListener接口,重写了contextInitialized(),调用initWebapplicationContext(),调用configAndRefreshWebApplicationContext()对IoC容器的配置和刷新操作,初始化IOC容器中的所有bean,默认以单例的形式初始化。
4、创建Filter
5、创建Servlet
springmvc容器的初始化和创建,其实就是Servlet的初始化和创建。springmvc容器就是DispatcherServlet,DispatcherServlet就是springmvc的前端控制器,也是spring容器的子容器。通过< load-on-startup > 来告知DispatcherServlet的创建时期,通过servlet里的< init-param> 知道springmvc容器的配置的位置 contextConfigLocation 初始化并创建springmvc容器并加载该容器里的bean。