springMVC中spring容器启动流程

来源:互联网 发布:怎么成为淘宝文案兼职 编辑:程序博客网 时间:2024/06/10 07:32

springMVC中spring容器启动过程,从web.xml配置文件开始分析。

<listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
ContextLoaderListener监听ServletContex的初始化,ServletContext初始化的时候调用继承自ContextLoader的initWebApplicationContext实例化spring容器,主要有两个方法createWebApplicationContext和configureAndRefreshWebApplicationContext前者创建XmlWebapplicationContext(web环境下spring Ioc容器),后者读取web.xml中的contextConfigLocation标签指定的xml文件并实例化文件中配置的bean在spring容器中注册,contextConfigLocation配置如下
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:XXX-service.xmlclasspath:XXX-dao.xml    </param-value></context-param>


如上例所示可以配置位于classpath路径中的xml文件,在一般j2ee工程中可以实现service,dao,controller,和web层分为不同的工程,只要保证这些工程正确的依赖关系并被web容器加载。<span style="font-family: Arial, Helvetica, sans-serif;">创建的XmlWebapplicationContext会被注册到ServletContext中,要想获取它的实例可以通过springmvc提供的WebApplicationContextUtils获取。</span>
                                             
0 0