@Test
public void test() {
FrameworkServlet frameworkServlet = new FrameworkServlet();
frameworkServlet.configureAndRefreshWebApplicationContext();
frameworkServlet.abstractApplicationContext.onRefresh();
}
在frameworkServlet的构造中,初始化了AbstractApplicationContext
public FrameworkServlet() {
this.abstractApplicationContext = new AbstractApplicationContext();
}
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;
}
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设计的神奇之处,让我们初始化其他事情后,再回调开始的方法,继续执行。