代码中是否有一些设计模式?

时间:2014-05-23 05:45:18

标签: java design-patterns

我看起来是春天的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());?

使用有什么好处吗?

1 个答案:

答案 0 :(得分:3)

我不知道您在哪里找到该代码,但是Spring 3.0.5 ContextLoaderListener的来源(例如,请参阅here)有以下代码:

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

你遗漏了方法的第一行!