`

ServletContext与ApplicationContext

 
阅读更多
ServletContext,即Servlet环境对象或Servlet容器,包含从容器环境中获得的初始化信息,其内提供的属性和方法在同一web应用下的所有servelt中被使用。每一个web-app只能有一个ServeltContext,web-app可以是一个放置web application文件的文件夹,也可以是一个.war。

    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传入就可以了。


查看另外两篇文章帮助理解:
Request、Session、servletContext区别
取WebApplicationContext方法集

分享到:
评论

相关推荐

    Request&Response编程

    WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。 由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象...

    在action以外的地方获取dao

    这是在action以外的地方拿ApplicationContext的方法,需要的参数是:ServletContext,在request.getServletContext里能拿到,所以只要有request就能拿到spring配置文件里的bean. 这种方法通常在写组件时用,比如写...

    spring源代码解析

    简单的说,在web容器中,通过ServletContext为Spring的IOC容器提供宿主环境,对应的建立起一个IOC容器的体系。其中,首先需要建立的是根上下文,这个上下文持有的对象可以有业务对象,数据存取对象,资源,事物管理...

    SpringMVC中的RootApplicationContext上下文和WebApplicationContext上下文,通过注解配置SpringMVC的完整解决方案

    注解配置SpringMVC原理简述1. 准备知识1.1 两个应用上下文1.2 ServletContext配置方法(Configuration Methods)1.3 运行时插拔1.4 SpringServletContainerInitializer1.4.1 AbstractContextLoaderInitializer1.4.2 ...

    S2SH整合报错

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed;...

    Tomcat系统架构与设计模式,第2部分:设计模式分析

    门面设计模式门面设计模式在Tomcat中有多处使用,在Request和Response对象封装中、StandardWrapper到ServletConfig封装中、ApplicationContext到ServletContext封装中等都用到了这种设计模式。门面设计模式的原理...

    粗浅看Tomcat中设计模式分析

    外观设计模式在Tomcat中有多处使用,在Request和Response对象封装中、StandardWrapper到ServletConfig封装中、ApplicationContext到ServletContext封装中等都用到了这种设计模式。这么多场合都用到了这种设计模式,...

    Java中的Listener监听器

     ServletContext监听  Session监听  Request监听  · Listener的应用实例  利用HttpSessionListener统计多在线用户人数  Spring使用ContextLoaderListener加载ApplicationContext配置信息  Spring使用...

    JSP 获取Spring 注入对象示例

    &lt;&#37;@ page import=org.springframework.web.context...ServletContext sc = this.getServletConfig().getServletContext(); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);

    Struts2+Spring3+MyBatis3完整实例

    - Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher.xml] - JSR-330 'javax.inject.Named' annotation found and supported for component scanning - JSR-330 'javax.inject....

    Spring中文帮助文档

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader接口 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    Spring API

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader接口 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    Spring 2.0 开发参考手册

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    spring chm文档

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    SPRING API 2.0.CHM

    ApplicationContext ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    4.3.4. ServletContextResource 4.3.5. InputStreamResource 4.3.6. ByteArrayResource 4.4. ResourceLoader 4.5. ResourceLoaderAware 接口 4.6. 把Resource作为属性来配置 4.7. Application context 和...

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    spring-framework-reference-4.1.2

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

Global site tag (gtag.js) - Google Analytics