WebApplicationContext
2024-10-10 09:48:46
在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取出。
最新文章
- yii2 伪静态配置
- 字符串专题:map POJ 1002
- 新浪微博客户端(63)-使用block进行链式编程
- HDOJ 1907 John
- Oracle V$SESSION详解
- AppClassLoader和WebAppClasssLoader的坑
- IE低版本兼容的感悟
- 大数据学习(二)-------- MapReduce
- Vue 组件(上)转载
- PSexec以及xcopy的简单使用
- 【Redis】Redis学习(一) Redis初步入门
- nRF2401A/nRF24L01/nRF24L01+无线模块最常见问题汇集(转)
- java中GZIPOutputStream 流的使用(EOFException)
- 数据仓库基础(八)Informatica 小例子
- SDL2中文教程
- 基于IDEA和Maven的SSM分层项目搭建
- Ubuntu12.04 安装nginx和mongo过程
- 【BZOJ1926】【SDOI2010】粟粟的书架 [主席树]
- EasyUI学习总结(四)——parser源码分析(转载)
- Largest Rectangle in Histogram, 求矩形图中最大的长方形面积
热门文章
- MongoDB 分片集群配置
- 什么是SSL双向认证,与单向认证证书有什么区别?
- PyQt(Python+Qt)学习随笔:使用QFontDialog.getFont交互设置应用或部件的字体
- PyQt(Python+Qt)学习随笔:Qt Designer中QAbstractButton派生按钮部件的icon属性和iconSize属性
- Python接口测试-使用requests模块发送post请求
- Java基础学习之HelloWorld(2)
- Server returns invalid timezone. Go to &#39;Advanced&#39; tab and set &#39;serverTimezone&#39; property manually.
- PostgreSQL 如何忽略事务中错误
- #2020征文-开发板#SYS_RUN()和MODULE_INIT()之间的那些事
- 开始是为了结束,结束是新的开始——NOI 2020 游记