在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取出。

最新文章

  1. yii2 伪静态配置
  2. 字符串专题:map POJ 1002
  3. 新浪微博客户端(63)-使用block进行链式编程
  4. HDOJ 1907 John
  5. Oracle V$SESSION详解
  6. AppClassLoader和WebAppClasssLoader的坑
  7. IE低版本兼容的感悟
  8. 大数据学习(二)-------- MapReduce
  9. Vue 组件(上)转载
  10. PSexec以及xcopy的简单使用
  11. 【Redis】Redis学习(一) Redis初步入门
  12. nRF2401A/nRF24L01/nRF24L01+无线模块最常见问题汇集(转)
  13. java中GZIPOutputStream 流的使用(EOFException)
  14. 数据仓库基础(八)Informatica 小例子
  15. SDL2中文教程
  16. 基于IDEA和Maven的SSM分层项目搭建
  17. Ubuntu12.04 安装nginx和mongo过程
  18. 【BZOJ1926】【SDOI2010】粟粟的书架 [主席树]
  19. EasyUI学习总结(四)——parser源码分析(转载)
  20. Largest Rectangle in Histogram, 求矩形图中最大的长方形面积

热门文章

  1. MongoDB 分片集群配置
  2. 什么是SSL双向认证,与单向认证证书有什么区别?
  3. PyQt(Python+Qt)学习随笔:使用QFontDialog.getFont交互设置应用或部件的字体
  4. PyQt(Python+Qt)学习随笔:Qt Designer中QAbstractButton派生按钮部件的icon属性和iconSize属性
  5. Python接口测试-使用requests模块发送post请求
  6. Java基础学习之HelloWorld(2)
  7. Server returns invalid timezone. Go to &#39;Advanced&#39; tab and set &#39;serverTimezone&#39; property manually.
  8. PostgreSQL 如何忽略事务中错误
  9. #2020征文-开发板#SYS_RUN()和MODULE_INIT()之间的那些事
  10. 开始是为了结束,结束是新的开始——NOI 2020 游记