SpringMVC中的ContextLoaderListener设计困惑

Easter79
• 阅读 493

最近在学习Spring MVC原理相关部分,就按顺序从ContextLoaderListener来看,但是我在阅读此类的设计中,遇到了些困惑的地方,现纪录如下,希望能遇到高手能帮我答疑解惑:

ContextLoaderListener 类的定义:

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
    private ContextLoader contextLoader;
    public ContextLoaderListener() {
    }

    public ContextLoaderListener(WebApplicationContext context) {
        super(context);
    }

    public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = createContextLoader();
        if (this.contextLoader == null) {
            this.contextLoader = this;
        }
        this.contextLoader.initWebApplicationContext(event.getServletContext());
    }

    @Deprecated
    protected ContextLoader createContextLoader() {
        return null;
    }

    @Deprecated
    public ContextLoader getContextLoader() {
        return this.contextLoader;
    }

    public void contextDestroyed(ServletContextEvent event) {
        if (this.contextLoader != null) {
            this.contextLoader.closeWebApplicationContext(event.getServletContext());
        }
        ContextCleanupListener.cleanupAttributes(event.getServletContext());
    }

}

困惑一:

ContextLoaderListener类已经继承了ContextLoader,为何又声明了一个私有的ContextLoader类的 对象属性contextLoader?而这个 contextLoader 在上下文的 意义愚以为仅仅是显示的说明 contextLoader 其实就是"this"。(我仿照ContextLoaderListener写了个去掉contextLoader属性的类,容器正常启动)

困惑二:

ContextLoaderListener doc注释为:

Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}...

Spring's root {@link WebApplicationContext},可以翻译为Spring的 顶级/根 WebApplicationContext吗?那是否有非root的呢?

也许我比较愚钝没能发现其中的奥秘,也可能我比较较真,非要打破沙锅问到底,思考了许久,还希望大牛能帮我答疑解惑。

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
uniapp安卓app打包后获取位置信息失败解决方法(含高德地图key详细申请流程)
这个也是我在做uniapp混合app时遇到的问题,也是困惑了几天,网上也有很大相关文章,但是有些地方有些不清晰,让新手很困惑。问题原因我在项目中碰到两个问题1.如果只需要用定位功能获取到经纬度坐标的,可以不用在manifest.jsonAppSDK配置中勾选定位和地图服务(注意:部分国产Android手机可能必须勾
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
2星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
从Nacos客户端视角来分析配置中心原理
Hello,大家好,我是麦洛,今天带大家一起从Nacos客户端视角来看看配置中心实现原理;整理这篇文章时候,也参照学习了部分大佬的博客,这里致谢;大家在阅读过程中如果发现错误或者问题,可以私信我交流,一起交流学习,一起进步;在开始阅读文章之前,有些思路我按我的理解先阐述一些,方便大家更快理清思路,不对的地方还请大家批评指正;1.Nacos客户端会在
Easter79 Easter79
2年前
SpringMVC开发中遇到的异常1:No primary or default constructor found for interface java.util.List
SpringMVC开发中遇到的异常1:Noprimaryordefaultconstructorfoundforinterfacejava.util.List参考文章:(1)SpringMVC开发中遇到的异常1:Noprimaryordefaultconstructorfoundforinterfacejav
Stella981 Stella981
2年前
Spring MVC的web.xml文件的分析
web.xml<listener<listenerclassorg.springframework.web.context.ContextLoaderListener</listenerclass</listener<contextparam<paramnamec
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
Spring4+Spring MVC+MyBatis整合思路
1、Spring框架的搭建这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件,那么spring容器搭建完成。(当然org.springframework的核心jar包需要引入)当然为了更加易用支持J2EE应用,一般我们还会加上如下
Stella981 Stella981
2年前
ContextLoaderListener ClassNotFoundException
在初学使用maven构建javaWeb的项目的时候,启动tomcat加载时,总是提示如下错误,辛苦一番终于找到解决办法。严重:Errorconfiguringapplicationlistenerofclassorg.springframework.web.context.ContextLoaderListenerJava(https://
Stella981 Stella981
2年前
25岁Java工程师如何转型学习人工智能?
收到不少童鞋的来信,其中以职业发展、技术成长的困惑居多。今天选择了一个颇具有代表性的问题:关于目前大热的AI入门学习,希望能帮助有同样问题的童鞋解惑指路。来信问题:25岁Java工程师如何转型学习AI?我是一名25岁的Java开发工程师。本科学习的专业是信息与计算科学(数学专业),因为对计算机方面感兴趣,之后培训学习了Java,所以现在从
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k