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 ...
随机推荐
- 浅尝 Elastic Stack (五) Logstash + Beats + Kafka
在 Elasticsearch.Kibana.Beats 安装 中讲到推荐架构: 本文基于 Logstash + Beats 读取 Spring Boot 日志 将其改为上述架构 如果没有安装 Kaf ...
- Spring Cloud 学习 (二) Ribbon
负载均衡是指将负载分摊到多个执行单元上,常见的负载均衡有两种方式:一种是独立进程单元,通过负载均衡策略,将请求转发到不同的执行单元上,例如 Ngnix:另一种是将负载均衡逻辑以代码的形式封装到服务消费 ...
- 20191209_Centos7.2使用yum安装mysql
1. 下载mysql的rpm包 [root@izwz91qnvovd6suufon1ccz ~]# wget http://dev.mysql.com/get/mysql57-community-re ...
- 第15.41节、PyQt(Python+Qt)入门学习:输入部件QComboBox组合框功能详解
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.概述 Designer中输入工具部件中的Combo Box组合框与 ...
- Python中使用f字符串进行字符串格式化的方法
在<第3.10节 Python强大的字符串格式化新功能:使用format字符串格式化>介绍了使用format进行字符串格式化的方法,在Python 3.6中,如果格式化字符串中的关键字参数 ...
- 2016 piapiapia 数组绕过
0x00.感悟 写完这道题,我感觉到了扫源码的重要性.暑假复现的那些CVE,有的就是任意文件读取,有的是任意命令执行,这些应该都是通过代码审计,得到的漏洞.也就和我们的CTF差不多了. ...
- bugkuctf web区 sql2
来了!终于做出来(虽然是在大佬帮助下,提前感谢大佬) 在看wp之后发现这是一道典型的.DS_Store源码泄露,其他类型的web源码泄露:https://www.secpulse.com/archiv ...
- 【科技】位运算(bitset)优化最长公共子序列算法
最长公共子序列(LCS)问题 你有两个字符串 \(A,B\),字符集为 \(\Sigma\),求 \(A, B\) 的最长公共子序列. 简单动态规划 首先有一个广为人知的 dp:\(f_{i,j}\) ...
- 题解-CF348E Pilgrims
题面 CF348E Pilgrims 有一棵 \(n\) 个点的 带权 树和 \(m\) 个关键点,要求杀了一个不关键的点,满足最多的关键点到离它最远的所有关键点的路径都被打断.求可以满足的最多关键点 ...
- AGC039D 题解
题目描述 给定在笛卡尔坐标系的单位圆上的\(N\)个点(圆心为\((0, 0)\)).第\(i\)个点的坐标为\((cos(\frac{2 \pi T_i}{L}), sin(\frac{2 \pi ...