我查看了Spring 3.05源代码,并找到了这个类。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener{
private ContextLoader contextLoader;
public void contextInitialized(ServletContextEvent event){
if(this.contextLoader == null){
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
}
为什么使用contextLoader字段,而不直接使用this.initWebApplicationContext(event.getServletContext())?
这种用法有什么好处吗?
ContextLoaderListener
不是ContextLoader
的子类,并且它有一个方法createContextLoader()
(现在已弃用),该方法返回ContextLoader
的实例。在当前状态下,它扩展了ContextLoader
,因此引用了this
。请参见此链接。 - Sundeep