Spring MVC是一个MVC模式的实现,在使用Spring MVC 时,主要需要在web.xml配置文件中设置DispatcherServlet,这个Servlet是实现Spring mvc 的前端控制器,所有的Web请求都需要通过它来处理,进行匹配、转发、数据处理。DispatcherServlet是实现Spring MVC最核心的部分。
在使用SpringMVC 时我们通常需要如下配置:
<servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Web容器启动,并开始加载各个Servlet,DispatcherServlet将建立自己的上下文来持有Spring MVC的bean对象,在建立这个自己持有的IoC容器时,会从ServletContext中得到根上下文作为DispatcherServlet持有的上下午的双亲上下文。有了这个根上下文,在对自己持有的上下文进行初始化,从而完成SpringMVC运行环境。在配置DispatcherServlet时可以设置初始化参数指定Ioc容器bean定义配置文件,名:<param-name>contextConfigLocation</param-name>,值:<param-value>classpath:springmvc.xml</param-value>,如果不指定,默认使用/WEB-INF/DispatcherServlet名-servlet.xml。上述配置如不指定 <init-param>,将使用/WEB-INF/mvc-dispatcher-servlet.xml。
1、DispatcherServlet类继承体系
DispatcherServlet通过继承FrameworkServlet和HttpServletBean而继承了HttpServlet,通过使用ServletAPI来对HTTP请求进行处理成为了Spring MVC的前端处理器,同时也实现了MVC模块与WEB容器集成。
2、DispatcherServlet的启动和初始化
DispatcherServlet启动
首先作为一个Servlet,在其生命周期中,将调用init()方法,完成初始化相关工作,init()方法在HttpServletBean中进行了重写,代码如下:
@Override public final void init() throws ServletException { if (logger.isDebugEnabled()) { logger.debug("Initializing servlet '" + getServletName() + "'"); } //获取Servlet的初始化参数,并进行设置 // Set bean properties from init parameters. try { PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); initBeanWrapper(bw); bw.setPropertyValues(pvs, true); } catch (BeansException ex) { logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex); throw ex; } //这里调用子类FrameWorkServlet中重写的initServletBean()方法继续进行初始化 // Let subclasses do whatever initialization they like. initServletBean(); if (logger.isDebugEnabled()) { logger.debug("Servlet '" + getServletName() + "' configured successfully"); } }
在FrameWorkServlet类中,initServletBean()方法如下:
@Override protected final void initServletBean() throws ServletException { getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'"); if (this.logger.isInfoEnabled()) { this.logger.info("FrameworkServlet '" + getServletName() + "': initialization started"); } long startTime = System.currentTimeMillis(); try { //这里通过调用initWebApplicationContext(),初始化web应用上下文 this.webApplicationContext = initWebApplicationContext(); initFrameworkServlet(); } catch (ServletException ex) { this.logger.error("Context initialization failed", ex); throw ex; } catch (RuntimeException ex) { this.logger.error("Context initialization failed", ex); throw ex; } if (this.logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; this.logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " + elapsedTime + " ms"); } }
继续看initWebApplicationContext()函数:
protected WebApplicationContext initWebApplicationContext() { //这里通过WebApplicationContextUtils工具类来获取根web应用上下文,这个上下文就是之前提到的全局应用根上下文,保存在ServletContext中,这个根上下文将作为当前mvc servlet上下文的双亲上下文。 WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); WebApplicationContext wac = null; if (this.webApplicationContext != null) { // A context instance was injected at construction time -> use it wac = this.webApplicationContext; if (wac instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> set // the root application context (if any; may be null) as the parent cwac.setParent(rootContext); } configureAndRefreshWebApplicationContext(cwac); } } } if (wac == null) { // No context instance was injected at construction time -> see if one // has been registered in the servlet context. If one exists, it is assumed // that the parent context (if any) has already been set and that the // user has performed any initialization such as setting the context id wac = findWebApplicationContext(); } if (wac == null) { //这里真正创建需要与该Servlet相关联的WebApplicationContext // No context instance is defined for this servlet -> create a local one wac = createWebApplicationContext(rootContext); } if (!this.refreshEventReceived) { // Either the context is not a ConfigurableApplicationContext with refresh // support or the context injected at construction time had already been // refreshed -> trigger initial onRefresh manually here. //这里,ioc容器已经创建初始化完成,执行onRefresh(wac)进一步初始化MVC其他模块,后面说明 onRefresh(wac); } if (this.publishContext) { //这里将当前建立的上下文保存到ServletContext中,attrName 是与当前servlet名相关联,保证唯一性 // Publish the context as a servlet context attribute. String attrName = getServletContextAttributeName(); getServletContext().setAttribute(attrName, wac); if (this.logger.isDebugEnabled()) { this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() + "' as ServletContext attribute with name [" + attrName + "]"); } } return wac; }
下面来看这个当前上下文是如何创建的,createWebApplicationContext()函数如下:
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) { Class<?> contextClass = getContextClass(); if (this.logger.isDebugEnabled()) { this.logger.debug("Servlet with name '" + getServletName() + "' will try to create custom WebApplicationContext context of class '" + contextClass.getName() + "'" + ", using parent context [" + parent + "]"); } if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException( "Fatal initialization error in servlet with name '" + getServletName() + "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } //通过BeanUtils反射实例化具体的上下文对象,contextClass通过getContextClass()获取,取得的class是Class<?> DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class; ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); //对上下文对象进行设置 wac.setEnvironment(getEnvironment()); wac.setParent(parent); wac.setConfigLocation(getContextConfigLocation()); //这里对刚刚创建的WebApplicationContext,进行配置和初始化操作,完成容器的最终初始化 configureAndRefreshWebApplicationContext(wac); return wac; }
继续configureAndRefreshWebApplicationContext()函数:
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information if (this.contextId != null) { wac.setId(this.contextId); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName()); } } //设置ServletContext引用,以及其他对象的设置 wac.setServletContext(getServletContext()); wac.setServletConfig(getServletConfig()); wac.setNamespace(getNamespace()); wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener())); // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig()); } postProcessWebApplicationContext(wac); applyInitializers(wac); //嗲用容器的refresh()方法,完成日期初始化 wac.refresh(); }可以看到对上下文进行了相关配置,设置ServletContext,设置初始化参数等,最后通过调用容器的refresh()方法启动整个容器,就像一般的IoC容器初始化过程一样。
至此,web应用上下文的启动、初始化已经完成,DispatcherServlet运行MVC需要的IoC容器已经建立起来,但是DispatcherServlet作为Spring
MVC前端控制器,还需要初始化MVC需要的其他模块,回到initWebApplicationContext()函数,看到在wac
= createWebApplicationContext(rootContext)后面,调用了onRefresh(was)函数,onRefresh函数在DispatcherServlet类中重写,代码如下:
@Override protected void onRefresh(ApplicationContext context) { //调用initStrategies完成MVC模块初始化 initStrategies(context); }
可以看到MVC初始化是在DispatcherServlet的initStrategies()方法中完成,在该方法中初始化各种MVC框架实现元素,如至此request映射的HandlerMappings,以及handler适配处理器HandlerAdapters,以及视图生成器ViewResolver等。
protected void initStrategies(ApplicationContext context) { initMultipartResolver(context); initLocaleResolver(context); initThemeResolver(context); initHandlerMappings(context); initHandlerAdapters(context); initHandlerExceptionResolvers(context); initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); }
比如初始化HandlerMappings,在SpringMVC中,HandlerMappings的作用是为Http请求找到对应的Controller控制器,来进一步处理请求。
private void initHandlerMappings(ApplicationContext context) { this.handlerMappings = null; //这里根据detectAllHandlerMappings布尔值确定是否导入所有的handlerMappings ,默认导入所有handlerMappings bean,这些bean可以来自于当前上下文容器,也可以来自其双亲根web上下文容器 if (this.detectAllHandlerMappings) { // Find all HandlerMappings in the ApplicationContext, including ancestor contexts. Map<String, HandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false); if (!matchingBeans.isEmpty()) { this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values()); // We keep HandlerMappings in sorted order. OrderComparator.sort(this.handlerMappings); } } else { try { HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class); this.handlerMappings = Collections.singletonList(hm); } catch (NoSuchBeanDefinitionException ex) { // Ignore, we'll add a default HandlerMapping later. } } //如果没有发现配置的handlerMappings ,需要设置默认的handlerMappings ,这些默认的handlerMappings 通过配置文件,在jar包中指定,在DispatcherServlet.properties配置文件中默认设置BeanNameUrlHandlerMapping、DefaultAnnotationHandlerMapping // Ensure we have at least one HandlerMapping, by registering // a default HandlerMapping if no other mappings are found. if (this.handlerMappings == null) { this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class); if (logger.isDebugEnabled()) { logger.debug("No HandlerMappings found in servlet '" + getServletName() + "': using default"); } } }
DispatcherServlet.properties默认配置文件:
# Default implementation classes for DispatcherServlet's strategy interfaces. # Used as fallback when no matching beans are found in the DispatcherServlet context. # Not meant to be customized by application developers. org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping, org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter, org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter, org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver, org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver, org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager