Java 代码中是否有一些设计模式?
我看了Spring3.05的源码,发现了这个类Java 代码中是否有一些设计模式?,java,design-patterns,Java,Design Patterns,我看了Spring3.05的源码,发现了这个类 public class ContextLoaderListener extends ContextLoader implements ServletContextListener{ private ContextLoader contextLoader; public void contextInitialized(ServletContextEvent event){ if(this.contextLoader
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字段,为什么不只使用这个.initWebApplicationContext(event.getServletContext())
使用该软件有什么好处吗?我不知道您在哪里找到了该代码,但是Spring 3.0.5的
ContextLoaderListener
的源代码(例如,请参阅)包含以下代码:
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
if (this.contextLoader == null) {
this.contextLoader = this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
你漏掉了方法的第一行 好问题。我看不出有什么好处。但是查看源代码历史,在Spring 2.5.6中,我看到
ContextLoaderListener
不是ContextLoader
的子类,它有一个方法createContextLoader()
(现在已弃用),该方法返回了ContextLoader
的实例。在当前状态下,它扩展了ContextLoader
,从而扩展了此引用。查看此问题似乎与主题无关,因为它应该属于代码审阅堆栈交换站点。createContextLoader()
已被弃用,并将最终被删除removed@Sundeep-OP专门询问3.0.5,它在代码中的位置(尽管不推荐)。Oops。我的错。由于该方法已被弃用,因此该方法返回null。@Sundeep-在以后的版本中,整个方法体是一行:initWebApplicationContext(event.getServletContext())代码>。现在有意义了。谢谢