Springmvc(1)

SpringMVC容器初始化

核心DispatcherServlet的繼承關係 spring

HttpServletßHttpServletBeanßFramewokServletßDispatcherServlet mvc

當只使用DispatcherServlet建立spring容器時,步驟以下: app

在FrameworkServlet的initWebApplicationContext()方法中建立spring容器。同時在該類中定義內部類ContextRefreshListener實現ApplicationListener接口 ide

private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> { this

 

     public void onApplicationEvent(ContextRefreshedEvent event) { spa

         FrameworkServlet.this.onApplicationEvent(event); .net

     } 對象

} 繼承

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;

}

}

相關文章
相關標籤/搜索