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
- DispatcherServlet,ContextConfigListener,servletContext,applicationContext
- ApplicationContext 、servletcontext和DispatcherServlet
- pageContext/ServletContext/ApplicationContext/ServletActionContext
- ServletContext与ApplicationContext
- ApplicationContext和ServletContext
- Spring中DispatcherServlet、WebApplicationContext、ServletContext的关系
- spring中ContextLoaderListener和DispatcherServlet区别 ApplicationContext
- Could not open ServletContext resource [/WEB-INF/dispatcherservlet-servlet.xml]
- Could not open ServletContext resource [/WEB-INF/dispatcherservlet-servlet.xml]
- spring中DispatcherServlet、WebApplicationContext、ServletContext之间的关系
- spring中DispatcherServlet、WebApplicationContext、ServletContext之间的关系
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- 倒序打印链表节点值
- DataRobot开发
- com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: and
- 使用js中常用的鼠标事件实现一个拖拽的例子
- 【转】调用nvm ls-remote只返回N/A 解决方案
- DispatcherServlet,ContextConfigListener,servletContext,applicationContext
- OKHttp的使用
- 高性能网络调优
- 博客新人
- spring简略实现excel导入
- C# 使用SqlBulkCopy类批量复制大数据
- 微信APP支付开发步骤及要点
- 2017全球运维技术大会 12大技术专场等你来!
- 硬件三人行,运放基础第2讲听课笔记,电路分析基础(一)