WebApplicationContext
在Web应用中,我们会用到WebApplicationContext 用它来保存上下文信息
那么它set到ServletContext的过程是怎么样呢
1)通过WEB.XML中监听类
p.p1 { margin: 0; font: 11px Monaco }
span.s1 { color: rgba(0, 145, 147, 1) }
span.s2 { color: rgba(78, 145, 146, 1) }
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
p.p1 { margin: 0; font: 11px Monaco }
触发contextInitialized方法

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { throw new IllegalStateException( "Cannot initialize context because there is already a root application context present - " + "check whether you have multiple ContextLoader* definitions in your web.xml!"); } Log logger = LogFactory.getLog(ContextLoader.class); servletContext.log("Initializing Spring root WebApplicationContext"); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } long startTime = System.currentTimeMillis(); try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. if (this.context == null) { this.context = createWebApplicationContext(servletContext); } |
最后一行,又调用了createWebApplicationContext方法,我们再来看一下这个方法的代码:
|
1
2
3
4
5
6
7
8
|
protected WebApplicationContext createWebApplicationContext(ServletContext sc) { Class<?> contextClass = determineContextClass(sc); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); } return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); } |
通过代码可知,在这里返回了一个ConfigurableWebApplicationContext,再来看一下contextLoader的initWebApplicationContext方法中最关键的代码:
|
1
|
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); |
在这里把context存入servletContext中,所以以后要用到WebApplicationContext的时候可以从servletContext取出。
WebApplicationContext的更多相关文章
- spring context上下文(应用上下文webApplicationContext)(转载)
(此文转载:http://www.cnblogs.com/brolanda/p/4265597.html) 一.先说ServletContext javaee标准规定了,servlet容器需要在应用项 ...
- 项目部署到tomcat Root中后导致 WebApplicationContext 初始化两次的解决方法
上一篇文章刚说项目部署到tomcat的ROOT中,今天就发现一个问题.通过eclipse启动tomcat时候,WebApplicationContext 初始化两次: 现象: 通过eclipse控 ...
- spring项目的 context root 修改之后,导致 WebApplicationContext 初始化两次的解决方法
修改了 spring web 项目的 context root 为 / 之后,在启动项目时,会导致 WebApplicationContext 初始化两次,下面是其初始化日志: 第一次初始化: 四月 ...
- 项目tomcat启动停在Initializing Spring root WebApplicationContext
来源于:http://ourteam.iteye.com/blog/1270699 某日,再次启动项目,spring一直停在这一句: Initializing Spring root WebAppli ...
- Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not defined
I'm stucked in configuring my web.config file under a web forms project in order to get an instance ...
- spring获取webapplicationcontext,applicationcontext几种方法详解
法一:在初始化时保存ApplicationContext对象代码: ApplicationContext ac = new FileSystemXmlApplicationContext(" ...
- Spring中DispacherServlet、WebApplicationContext、ServletContext的关系
转载:http://blog.csdn.net/c289054531/article/details/9196149?utm_source=tuicool&utm_medium=referra ...
- Spring中WebApplicationContext的研究
Spring中WebApplicationContext的研究 ApplicationContext是Spring的核 心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些 ...
- Initializing Spring root WebApplicationContext
最近 我部署ssh项目的时候经常出现这样的问题,我的解决办法是 log4j:WARN No appenders could be found for logger (org.springframewo ...
- Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系
在Web容器(比如Tomcat)中配置Spring时,你可能已经司空见惯于web.xml文件中的以下配置代码: <context-param> <param-name>cont ...
随机推荐
- moviepy简介及安装
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 MoviePy是一个用于视频编辑的Pyt ...
- PyQt(Python+Qt)学习随笔:怎么在QScrollArea滚动区域中展示子部件的超长内容?
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 滚动区域可以针对部署在其上的子部件在不可见时进行滚动展示,但这种滚动展示仅只能展示内容层可见范围的子 ...
- Mac下安装Mesa
下载Mesa源代码: git clone https://gitlab.freedesktop.org/mesa/mesa.git 如果下载太慢,请参看 下载国外资源. 我下载后看到的Mesa版本信息 ...
- LZZ磁力资源搜索4.2.2,整合多个站点,大部分资源都能搜到
资源搜索 4.2.2.20200310网友提出的功能已完成1:新增 时间日期排序,单击表头或右键菜单选择,即可自动排序2:新增 搜索完毕 音效开启或关闭 选项3:资源站点 Sunyaa 由聚合站点改为 ...
- 团队作业 需求改进&系统设计
PaChat聊天系统 一.需求&原型改进: 1.针对课堂讨论环节老师和其他组的问题及建议,对修改选题及需求进行修改 问题1:功能划分条理不够清晰. 修改1:改为流程图的形式. 问题2:功能不能 ...
- selenium模拟淘宝登陆,过所有验证
淘宝模拟登陆实现 由于淘宝使用了滑动验证码,需要进行模糊手动滑动,因此考虑使用selenium+chromedriver进行模拟登陆. 淘宝的登陆网址:https://login.taobao.com ...
- js监测页面是否切换到后台
最近做个弹幕,用的是第三方的插件,在浏览器页面切换到后台,返回后发现数据有堆叠卡死的情况,如何解决这个问题?网上参考了些demo,大致可以实现 1.document.hidden( Boolean值, ...
- P4085 [USACO17DEC]Haybale Feast
我又开始水了,感觉又是一道虚假的蓝题 题意 非常好理解,自己看吧 题解 可以比较轻易的发现,如果对于一段满足和大于等于 \(m\) 的区间和其满足和大于等于 \(m\) 的子区间来说,选择子区间肯定是 ...
- python协程需要注意的
python协程需要注意的点 都在注释里 # -*- coding: utf-8 -*- import asyncio import time from geeker import schedule ...
- oracle 修改默认临时表空间
--查询当前数据库默认临时表空间名 select * from database_properties where property_name='DEFAULT_TEMP_TABLESPACE'; - ...