您的位置:首页 > 编程语言 > Java开发

SpringMVC中的ContextLoaderListener设计困惑

2015-12-25 00:07 267 查看
最近在学习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的呢?

也许我比较愚钝没能发现其中的奥秘,也可能我比较较真,非要打破沙锅问到底,思考了许久,还希望大牛能帮我答疑解惑。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ContextLoaderListener