Skip to content

Latest commit

 

History

History
119 lines (88 loc) · 4.13 KB

chapter13.md

File metadata and controls

119 lines (88 loc) · 4.13 KB

手写spring ApplicationListener

模拟spring servlet触发FrameworkServlet

    @Test
    public void test() {

        FrameworkServlet frameworkServlet = new FrameworkServlet();
        frameworkServlet.configureAndRefreshWebApplicationContext();
        frameworkServlet.abstractApplicationContext.onRefresh();
    }

new FrameworkServlet()

在frameworkServlet的构造中,初始化了AbstractApplicationContext

 public FrameworkServlet() {
        this.abstractApplicationContext = new AbstractApplicationContext();
    }

frameworkServlet.configureAndRefreshWebApplicationContext()

configureAndRefreshWebApplicationContext方法中进行了监听器创建和添加。

  public void configureAndRefreshWebApplicationContext() {
       abstractApplicationContext.addApplicationListener(new SourceFilteringListener(abstractApplicationContext,new FrameworkServlet.ContextRefreshListener()));
    }

可以看到在abstractApplicationContext添加了一个监听器,那abstractApplicationContext中定义了对listener的处理,看下添加方法如何实现。

  private List<ApplicationListener> listeners = new CopyOnWriteArrayList<>();
    @Override
    public void addApplicationListener(ApplicationListener<?> listener) {

        listeners.add(listener);
    }

可以看出,是把监听器房到一个list集合中。

再回到方法configureAndRefreshWebApplicationContext中,看到添加的监听器是,SourceFilteringListener, 参数是abstractApplicationContext,ContextRefreshListener,而在sourceFilteringListener的构造函数中,又进行了参数处理。

 public SourceFilteringListener(Object source,ApplicationListener<?> delegate) {
        this.source = source;
        this.delegate = new GenericApplicationListenerAdapter(delegate);
    }

也就是说SourceFilteringListener只是转发我们的监听器,真正的处理方法,在GenericApplicationListenerAdapter 中。

private final ApplicationListener<ApplicationEvent> delegate;

    public GenericApplicationListenerAdapter(ApplicationListener<?> delegate) {
        this.delegate = (ApplicationListener<ApplicationEvent>)delegate;
    }

frameworkServlet.abstractApplicationContext.onRefresh()

public void onRefresh() {
  for (ApplicationListener listener : listeners) {
    if (((GenericApplicationListener)listener).supportsSourceType(getClass())){
      listener.onApplicationEvent(new ContextRefreshedEvent());
    }

  }
}

循环遍历添加的监听器,判断监听器是否支持处理。

public boolean supportsSourceType(@Nullable Class<?> sourceType) {
  return (sourceType != null && sourceType.isInstance(this.source));
}

如果支持,则进行事件处理。又上述代码可知,listeners集合中只存储了一个对象SourceFilteringListener,所以这时执行的具体实现应该是SourceFilteringListener中的onApplicationEvent。

   public void onApplicationEvent(ApplicationEvent event) {
        delegate.onApplicationEvent(event);
    }

由上述SourceFilteringListener的构造可知,这是的delegate是构造中的GenericApplicationListenerAdapter,所以执行``GenericApplicationListenerAdapter中的onApplicationEvent`。

 public void onApplicationEvent(ApplicationEvent event) {
        delegate.onApplicationEvent(event);
    }

GenericApplicationListenerAdapter的构造可知,delegate是一开始传递的FrameworkServlet.ContextRefreshListener(),所以这是执行ContextRefreshListener中的onApplicationEvent的事件。

private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
  public ContextRefreshListener() {
  }
  @Override
  public void onApplicationEvent(ContextRefreshedEvent event) {
    onrefresh(event);
  }
}

可以看出,从FrameworkSerlvet出发,又绕回到了FrameworkServlet,这即是Spring设计的神奇之处,让我们初始化其他事情后,再回调开始的方法,继续执行。

项目地址