DispatcherServlet,ContextConfigListener,servletContext,applicationContext

来源:互联网 发布:mac玩游戏怎么全屏 编辑:程序博客网 时间:2024/06/08 13:58

ServletContext:

ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放。request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。所以,为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。

application:

其实servletContext和application 是一样的,就相当于一个类创建了两个不同名称的变量。在

servlet中ServletContext就是application对象。大家只要打开jsp编译过后生成的Servlet中的_jspService()方法就可以看到如下的声明:http://blog.csdn.net/lvzhiyuan/article/details/4664624

ServletContext application = null;            application = pageContext.getServletContext();
两者的区别就是application用在jsp中,servletContext用在servlet中。application和page

request session 都是JSP中的内置对象,在后台用ServletContext存储的属性数据可以用application对象获得。

applicationContext:

ApplicationContext 是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出

WebApplicationContext:

WebApplicationContext继 承自ApplicationContext,下面是获取WebApplicationContext的源码:

 public static WebApplicationContext getWebApplicationContext(ServletContext sc) {        return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);    }    public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName) {        Assert.notNull(sc, "ServletContext must not be null");        Object attr = sc.getAttribute(attrName);        if(attr == null) {            return null;        } else if(attr instanceof RuntimeException) {            throw (RuntimeException)attr;        } else if(attr instanceof Error) {            throw (Error)attr;        } else if(attr instanceof Exception) {            throw new IllegalStateException((Exception)attr);        } else if(!(attr instanceof WebApplicationContext)) {            throw new IllegalStateException("Context attribute is not of type WebApplicationContext: " + attr);        } else {            return (WebApplicationContext)attr;        }    }
可以看出WebApplicationContext存放于ServletContext的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性中

String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";


spring 将 applicationContext存放在ServletContext中key值为"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中

DispatcherServlet加载的context成功后如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中

例如 web.xml中配置如下

<servlet>      <servlet-name>mvcServlet</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>classpath*:/spring/config/applicationContextMVC.xml</param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>
则对应的applicationContext的attribute key值为org.springframework.web.servlet.FrameworkServlet.CONTEXT.mvcServlet

在每次request请求时,DispatcherServlet会将此applicationContext存放在request中attribute值为 org.springframework.web.servlet.DispatcherServlet.CONTEXT中(request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE,getWebApplicationContext());)。可以通过 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 来获取对应的applicationContext。

  从上面的分析可以看出

DispatcherServlet所加载的applicationContext可以认为是mvc私有的context,由于保存在servletContext中的key值与通过ContextLoaderListener加载进来的applicationContext使用的key值不相同,因此如果只使用DispatcherServlet加载context的话,如果程序中有地方使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 来试图获取applicationContext时,就会抛出"No WebApplicationContext found: no ContextLoaderListener registered?"的exception

参考文章:http://www.cnblogs.com/onlywujun/articles/2869302.html
参考文章:http://blog.csdn.net/madun/article/details/8988860/

参考文章:http://blog.csdn.net/lvzhiyuan/article/details/4664624

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