我们对DispatcherServlet进行初始化的过程中,会有对他进行监听器的注册操作。
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
public void addApplicationListener(ApplicationListener<?> listener) {
if (this.applicationEventMulticaster != null) {
this.applicationEventMulticaster.addApplicationListener(listener);
}
else {
this.applicationListeners.add(listener);
}
}
在SouceFilteringListener中,它将具体的操作交给了他的代理实现。也就是我们当初传入的ContextRefreshListener实例。而这个类是FrameworkServlet的内部类。
protected void onApplicationEventInternal(ApplicationEvent event) {
if (this.delegate == null) {
throw new IllegalStateException(
"Must specify a delegate object or override the onApplicationEventInternal method");
}
this.delegate.onApplicationEvent(event);
}
private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(ContextRefreshedEvent event) {
FrameworkServlet.this.onApplicationEvent(event);
}
}
在FrameworkServlet中,执行onApplicationEvent方法,而其中的onRefresh在此类是空的,由DispatcherServlet覆盖。
public void onApplicationEvent(ContextRefreshedEvent event) {
this.refreshEventReceived = true;
onRefresh(event.getApplicationContext());
}
在DispatcherServlet中
@Override
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
/**
* Initialize the strategy objects that this servlet uses.
* <p>May be overridden in subclasses in order to initialize further strategy objects.
*/
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
所以总结就是在初始化Servlet的过程中,策略的初始化就已经开始了。并且是在Spring的AbstractApplicationContext的refresh方法执行完finishRefresh触发的。