核心DispatcherServlet的继承关系
HttpServletßHttpServletBeanßFramewokServletßDispatcherServlet
当只使用DispatcherServlet创建spring容器时,步骤如下:
在FrameworkServlet的initWebApplicationContext()方法中创建spring容器。同时在该类中定义内部类ContextRefreshListener实现ApplicationListener接口
private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
public void onApplicationEvent(ContextRefreshedEvent event) {
FrameworkServlet.this.onApplicationEvent(event);
}
}
onApplicationEvent()方法通过Spring容器的publishEvent()方法来触发,从而调用FrameworkServlet.this.onApplicationEvent(event)方法中定义的onRefresh(event.getApplicationContext())方法来初始化springmvc需要的组件
protected void initStrategies(ApplicationContext context) {
initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
}
那么在什么地方调用publishEvent()方法的呢,从initWebApplicationContext()的部分代码中可以看到
protected WebApplicationContext initWebApplicationContext() {
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
// No fixed context defined for this servlet - create a local one.
WebApplicationContext parent =
//当wac为空时,创建上下文,跟踪createWebApplicationContext方法,可以看到该//方法调用refresh方法,refresh方法中又调用finishRefresh(),在finishRefresh方法中
//调用publishEvent()方法从而触发ContextRefreshListener监听的事件
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
wac = createWebApplicationContext(parent);
}
if (!this.refreshEventReceived) {
//当wac不为空时,手动调用onRefresh()方法初始化springmvc的组件
onRefresh(wac);
}
下面演示一个ApplicationLIstener监听器的例子
MyListener(实现ApplicationListener接口)
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import event.MyEvent;
@Component
public class MyListener implements ApplicationListener<MyEvent> {
public MyListener(){
System.out.println("myListener构造方法");
}
@Override
public void onApplicationEvent(MyEvent event) {
// TODO Auto-generated method stub
System.err.println("myEvent事件触发了:"+event.getName());
}
}
MyEvent(继承ApplicationEvent类)
import org.springframework.context.ApplicationEvent;
public class MyEvent extends ApplicationEvent {
/**
* @param source
*/
public MyEvent(Object source) {
super(source);
// TODO Auto-generated constructor stub
}
private String name="Lee";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
TestListener(实现InitializingBean和ApplicationContextAware接口,实现InitializingBean接口的Bean其afterPropertiesSet方法在其初始化时会被执行,实现ApplicationContextAware接口可以获得容器对象)
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import event.MyEvent;
@Component
public class TestListener implements InitializingBean,ApplicationContextAware{
private ApplicationContext context;
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("afterPropertiesSet方法");
System.out.println(this.context);
this.context.publishEvent(new MyEvent(new Object()));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
this.context = applicationContext;
}
}