在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. Fiddler 4的安装

    1.双击FiddlerSetup_2.0.20194.413.exe安装包 2点击同意 3.傻瓜式安装即可 4.安装完就是这个样子 5.然后点这个 然后不管出现什么都点yes(或者是),最后点击ok ...

  2. 部署完的Django项目升级为HTTPS

    1.阿里云上申请免费ssl证书--->提交各种资料--->等待审核--->下载证书. 2.远程连接阿里云服务器,将下载下来的证书内容复制到Nginx安装目录下的cert目录(需要新建 ...

  3. 查询Oracle日志文件的方法

    Oracle日志文件相信经常使用Oracle数据库的朋友都比较熟悉了,下面将为您介绍的是查询Oracle日志文件的几种方法,供您参考学习. 1.查询系统使用的是哪一组日志文件: select * fr ...

  4. 第3.3节 强大的Python列表

    一. 列表切片操作补充 列表切片支持所有序列切片的方法,以倒序切片和步长大于1的情况再举例验证一下: l=[1,2,3,4,5] l[::2] #结果[1, 3, 5] l[-1::2] #结果[5] ...

  5. tesseract-ocr 图片文字识别

    本篇记录下python识别图片中的文字 所需的安装配置:  安装库: pip install pytesseract pip install PILLOW   安装 Tesseract-OCR软件: ...

  6. 公司只提供签名服务,不提供证书文件,如何打包Electron应用

    需求 稍微正规点的公司,都要为自己开发的软件做代码签名,如下图所示 代码签名的主要目的是为了确保软件的来源(这个软件是由谁生产的)和软件的内容不被篡改 一个软件公司可能有很多团队,很多开发者,开发不同 ...

  7. 百度前端技术学院-基础-day25-27

    倒数开始 滴答滴 滴答滴 task1 题目: 我们现在来做一个最简单的时钟,通过小练习来学习 Date,复习定时,然后再练习一下函数的封装具体需求如下: 在页面中显示当前日期及时间,按秒更新 格式为 ...

  8. js 导出div 中的类容为 word 文件

    //引入包 <script src="/FileSaver.js"></script>  <script src="/jquery.word ...

  9. 算法——K 个一组翻转链表

    给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度. 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序. 示例: 给你这个 ...

  10. Jackson:我是最牛掰的 Java JSON 解析器(有点虚)

    在当今的编程世界里,JSON 已经成为将信息从客户端传输到服务器端的首选协议,可以好不夸张的说,XML 就是那个被拍死在沙滩上的前浪. 很不幸的是,JDK 没有 JSON 库,不知道为什么不搞一下.L ...