在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的更多相关文章

  1. spring context上下文(应用上下文webApplicationContext)(转载)

    (此文转载:http://www.cnblogs.com/brolanda/p/4265597.html) 一.先说ServletContext javaee标准规定了,servlet容器需要在应用项 ...

  2. 项目部署到tomcat Root中后导致 WebApplicationContext 初始化两次的解决方法

    上一篇文章刚说项目部署到tomcat的ROOT中,今天就发现一个问题.通过eclipse启动tomcat时候,WebApplicationContext 初始化两次: 现象:   通过eclipse控 ...

  3. spring项目的 context root 修改之后,导致 WebApplicationContext 初始化两次的解决方法

    修改了 spring web 项目的 context root 为 / 之后,在启动项目时,会导致 WebApplicationContext  初始化两次,下面是其初始化日志: 第一次初始化: 四月 ...

  4. 项目tomcat启动停在Initializing Spring root WebApplicationContext

    来源于:http://ourteam.iteye.com/blog/1270699 某日,再次启动项目,spring一直停在这一句: Initializing Spring root WebAppli ...

  5. 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 ...

  6. spring获取webapplicationcontext,applicationcontext几种方法详解

    法一:在初始化时保存ApplicationContext对象代码: ApplicationContext ac = new FileSystemXmlApplicationContext(" ...

  7. Spring中DispacherServlet、WebApplicationContext、ServletContext的关系

    转载:http://blog.csdn.net/c289054531/article/details/9196149?utm_source=tuicool&utm_medium=referra ...

  8. Spring中WebApplicationContext的研究

    Spring中WebApplicationContext的研究 ApplicationContext是Spring的核 心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些 ...

  9. Initializing Spring root WebApplicationContext

    最近 我部署ssh项目的时候经常出现这样的问题,我的解决办法是 log4j:WARN No appenders could be found for logger (org.springframewo ...

  10. Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系

    在Web容器(比如Tomcat)中配置Spring时,你可能已经司空见惯于web.xml文件中的以下配置代码: <context-param> <param-name>cont ...

随机推荐

  1. 第2.3节 Python运算符大全

    一. Python的算术运算 Python的算术运算符与C语言类似,略有不同.包括加(+).减(-).乘(*).除(/).取余(%).按位或(|).按位与(&).按位求补(~).左移位(< ...

  2. 第15.23节 PyQt(Python+Qt)入门学习:Model/View架构中QListView视图配套Model的开发使用

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 QListView理论上可以和所有QAbstractItemModel派生的类如QStri ...

  3. PyQt(Python+Qt)学习随笔:desktop的frameGeometry、frameSize、availableGeometry,screenGeometry

    frameGeometry:返回窗口相对于父窗口的几何形状的大小,包括窗口的框架,当窗口是顶级窗口时,返回的实际上是屏幕的大小: frameSize:返回窗口的几何形状的大小,包括窗口的框架,当窗口是 ...

  4. POJ_1961

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 19817   Accepted: 9640 Descripti ...

  5. pandas 由其中几列生成新的列

    data是一个dataframe #data["x1"]=data[["a","b"]].apply(lambda x:x["a& ...

  6. Newbe.ObjectVisitor 0.4.4 发布,模型验证器上线

    Newbe.Claptrap 0.4.4 发布,模型验证器上线. 更新内容 完全基于表达式树的模型验证器 本版本,我们带来了基于表达式树实现的模型验证器.并实现了很多内置的验证方法. 我们罗列了与 F ...

  7. CF1416D Graph and Queries

    本题解用于作者加深算法印象,也欢迎各位的阅读. 题目大意 给你一张无向图,并给你两种操作: \(1~v\) :找到当前点 \(v\) 所在的联通块内权值最大的点,输出该点权值并将其权值改为 \(0\) ...

  8. 笔记-[SDOI2012]任务安排

    笔记-[SDOI2012]任务安排 [SDOI2012]任务安排 \(f_i\) 表示分配到第 \(i\) 个任务的最小费用. 令 \(st_i=\sum_{h=1}^iT_h\),\(sc_i=\s ...

  9. 使用 open 函数 写的代码 用户名登录

    先创建文件ha.log 内容: aaa$$123bbb$$456 def dl(user,pas): f = open('ha.log', 'r', encoding="utf-8" ...

  10. vs2015常用快捷键大全

    1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键"Ctrl + -"; 2)前进到下一个光标位置:"Ctrl + Shift + - &q ...