ApplicationContext 、servletcontext和DispatcherServlet

来源:互联网 发布:短信数据恢复 编辑:程序博客网 时间:2024/06/16 12:52

转自:http://www.cnblogs.com/onlywujun/articles/2869302.html

1.  ServletContext

ServletContext,即Servlet环境对象或Servlet容器,包含从容器环境中获得的初始化信息,其内提供的属性和方法在同一web应用下的所有servelt中被使用。每一个web-app只能有一个ServeltContext,web-app可以是一个放置web application文件的文件夹,也可以是一个.war。 
2.ApplicationContext 
    ApplicationContext 是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应 用的容器”了:P,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出,虽然我没有看过这一部分的源代码,但我想它应该是一 个类似Map的结构。 
在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继 承自ApplicationContext,先让我们看看在Web应用中,怎么初始化WebApplicationContext,在web.xml中定 义:

复制代码
<context-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/applicationContext.xml</param-value>      </context-param>           <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>   <servlet>      <servlet-name>context</servlet-name>      <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>      <load-on-startup>1</load-on-startup>      </servlet> 
复制代码

 

可以看出,有两种方法,一个是用ContextLoaderListener这个Listerner,另一个是ContextLoaderServlet这 个Servlet,这两个方法都是在web应用启动的时候来初始化WebApplicationContext,我个人认为Listerner要比 Servlet更好一些,因为Listerner监听应用的启动和结束,而Servlet得启动要稍微延迟一些,如果在这时要做一些业务的操作,启动的前 后顺序是有影响的。 
那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢? 以ContextLoaderListener为例,我们可以看到 
public void contextInitialized(ServletContextEvent event) { 
this.contextLoader = createContextLoader(); 
this.contextLoader.initWebApplicationContext(event.getServletContext()); 

protected ContextLoader createContextLoader() { 
return new ContextLoader(); 

  
    ContextLoader 是一个工具类,用来初始化WebApplicationContext,其主要方法就是initWebApplicationContext,我们继续追 踪initWebApplicationContext这个方法(具体代码我不贴出,大家可以看Spring中的源码),我们发现,原来 ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默认实现类)放在了 ServletContext中,ServletContext也是一个“容器”,也是一个类似Map的结构,而 WebApplicationContext在ServletContext中的KEY就是 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我们如果要使用 WebApplicationContext则需要从ServletContext取出,Spring提供了一个 WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把 ServletContext传入就可以了。

3.DispatcherServlet

当contextLoaderListener监听器初始化完毕后,开始初始化web.xml中配置的Servlet,这个servlet可以配置多个,以最常见的DispatcherServlet为例,这个servlet实际上是一个标准的前端控制器,用以转发、匹配、处理每个servlet请求。DispatcherServlet上下文在初始化的时候会建立自己的IoC上下文,用以持有spring mvc相关的bean。在建立DispatcherServlet自己的IoC上下文时,会利用WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先从ServletContext中获取之前的根上下文(即WebApplicationContext)作为自己上下文的parent上下文。有了这个parent上下文之后,再初始化自己持有的上下文。这个DispatcherServlet初始化自己上下文的工作在其initStrategies方法中可以看到,大概的工作就是初始化处理器映射、视图解析等。这个servlet自己持有的上下文默认实现类也是mlWebApplicationContext。初始化完毕后,spring以与servlet的名字相关(此处不是简单的以servlet名为Key,而是通过一些转换,具体可自行查看源码)的属性为属性Key,也将其存到ServletContext中,以便后续使用。这样每个servlet就持有自己的上下文,即拥有自己独立的bean空间,同时各个servlet共享相同的bean,即根上下文(第2步中初始化的上下文)定义的那些bean。


阅读全文
0 0
原创粉丝点击