首页 > 代码库 > HttpServletBean 、 FrameworkServlet 和 DispatcherServlet 关系
HttpServletBean 、 FrameworkServlet 和 DispatcherServlet 关系
DispatcherServlet初始化顺序
继承体系结构如下所示:
dispatcherServlet 继承FrameworkServlet 继承 HttpServletBean 继承 HttpServlet
HttpServletBean继承HttpServlet,因此在Web容器启动时将调用它的init方法,该初始化方法的主要作用:
将Servlet初始化参数(init-param)设置到该组件上(如contextAttribute、contextClass、namespace、contextConfigLocation),通过BeanWrapper简化设值过程,方便后续使用;
提供给子类初始化扩展点,initServletBean(),该方法由FrameworkServlet覆盖。
1 <!-- This servlet must be loaded first to configure the log4j 2 system and create the WebApplicationContext 3 --> 4 <servlet> 5 <servlet-name>config</servlet-name> 6 <servlet-class>org.springframework.framework.web.context.ContextLoaderServlet</servlet-class> 7 <init-param> 8 <param-name>contextClass</param-name> 9 <param-value>org.springframework.framework.web.context.XMLWebApplicationContext</param-value> 10 </init-param> 11 <init-param> 12 <param-name>log4jPropertiesUrl</param-name> 13 <param-value>/WEB-INF/log4j_PRODUCTION.properties</param-value> 14 </init-param> 15 <!-- This is essential --> 16 <load-on-startup>1</load-on-startup> 17 </servlet>
1 public abstract class HttpServletBean extends HttpServlet implements EnvironmentAware{ 2 @Override 3 public final void init() throws ServletException { 4 //省略部分代码 5 //1、如下代码的作用是将Servlet初始化参数设置到该组件上 6 //如contextAttribute、contextClass、namespace、contextConfigLocation; 7 try { 8 PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); 9 BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); 10 ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); 11 bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment)); 12 initBeanWrapper(bw); 13 bw.setPropertyValues(pvs, true); 14 } 15 catch (BeansException ex) { 16 //…………省略其他代码 17 } 18 //2、提供给子类初始化的扩展点,该方法由FrameworkServlet覆盖 19 initServletBean(); 20 if (logger.isDebugEnabled()) { 21 logger.debug("Servlet ‘" + getServletName() + "‘ configured successfully"); 22 } 23 } 24 //…………省略其他代码 25 }
2、FrameworkServlet继承HttpServletBean,通过initServletBean()进行Web上下文初始化,该方法主要覆盖一下两件事情:
初始化web上下文;
提供给子类初始化扩展点;
1 public abstract class FrameworkServlet extends HttpServletBean { 2 @Override 3 protected final void initServletBean() throws ServletException { 4 //省略部分代码 5 try { 6 //1、初始化Web上下文 7 this.webApplicationContext = initWebApplicationContext(); 8 //2、提供给子类初始化的扩展点 9 initFrameworkServlet(); 10 } 11 //省略部分代码 12 } 13 }
1 protected WebApplicationContext initWebApplicationContext() { 2 //ROOT上下文(ContextLoaderListener加载的) 3 WebApplicationContext rootContext = 4 WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 5 WebApplicationContext wac = null; 6 if (this.webApplicationContext != null) { 7 // 1、在创建该Servlet注入的上下文 8 wac = this.webApplicationContext; 9 if (wac instanceof ConfigurableWebApplicationContext) { 10 ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac; 11 if (!cwac.isActive()) { 12 if (cwac.getParent() == null) { 13 cwac.setParent(rootContext); 14 } 15 configureAndRefreshWebApplicationContext(cwac); 16 } 17 } 18 } 19 if (wac == null) { 20 //2、查找已经绑定的上下文 21 wac = findWebApplicationContext(); 22 } 23 if (wac == null) { 24 //3、如果没有找到相应的上下文,并指定父亲为ContextLoaderListener 25 wac = createWebApplicationContext(rootContext); 26 } 27 if (!this.refreshEventReceived) { 28 //4、刷新上下文(执行一些初始化) 29 onRefresh(wac); 30 } 31 if (this.publishContext) { 32 // Publish the context as a servlet context attribute. 33 String attrName = getServletContextAttributeName(); 34 getServletContext().setAttribute(attrName, wac); 35 //省略部分代码 36 } 37 return wac; 38 }
从initWebApplicationContext()方法可以看出,基本上如果ContextLoaderListener加载了上下文将作为根上下文(DispatcherServlet的父容器)。
最后调用了onRefresh()方法执行容器的一些初始化,这个方法由子类实现,来进行扩展。
3、DispatcherServlet继承FrameworkServlet,并实现了onRefresh()方法提供一些前端控制器相关的配置:
1 public class DispatcherServlet extends FrameworkServlet { 2 //实现子类的onRefresh()方法,该方法委托为initStrategies()方法。 3 @Override 4 protected void onRefresh(ApplicationContext context) { 5 initStrategies(context); 6 } 7 8 //初始化默认的Spring Web MVC框架使用的策略(如HandlerMapping) 9 protected void initStrategies(ApplicationContext context) { 10 initMultipartResolver(context); 11 initLocaleResolver(context); 12 initThemeResolver(context); 13 initHandlerMappings(context); 14 initHandlerAdapters(context); 15 initHandlerExceptionResolvers(context); 16 initRequestToViewNameTranslator(context); 17 initViewResolvers(context); 18 initFlashMapManager(context); 19 } 20 }
从如上代码可以看出,DispatcherServlet启动时会进行我们需要的Web层Bean的配置,如HandlerMapping、HandlerAdapter等,而且如果我们没有配置,还会给我们提供默认的配置。
从如上代码我们可以看出,整个DispatcherServlet初始化的过程和做了些什么事情,具体主要做了如下两件事情:
1、初始化Spring Web MVC使用的Web上下文,并且可能指定父容器为(ContextLoaderListener加载了根上下文);
2、初始化DispatcherServlet使用的策略,如HandlerMapping、HandlerAdapter等。
服务器启动时的日志分析(此处加上了ContextLoaderListener从而启动ROOT上下文容器):
信息: Initializing Spring root WebApplicationContext //由ContextLoaderListener启动ROOT上下文
2012-03-12 13:33:55 [main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
2012-03-12 13:33:55 [main] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Mon Mar 12 13:33:55 CST 2012]; root of context hierarchy
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader - Loading bean definitions
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 0 bean definitions from location pattern [/WEB-INF/ContextLoaderListener.xml]
2012-03-12 13:33:55 [main] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Bean factory for Root WebApplicationContext: org.springframework.beans.factory.support.DefaultListableBeanFactory@1c05ffd: defining beans []; root of factory hierarchy
2012-03-12 13:33:55 [main] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Bean factory for Root WebApplicationContext:
2012-03-12 13:33:55 [main] DEBUG org.springframework.web.context.ContextLoader - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT] //将ROOT上下文绑定到ServletContext
2012-03-12 13:33:55 [main] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 438 ms //到此ROOT上下文启动完毕
2012-03-12 13:33:55 [main] DEBUG org.springframework.web.servlet.DispatcherServlet - Initializing servlet ‘chapter2‘
信息: Initializing Spring FrameworkServlet ‘chapter2‘ //开始初始化FrameworkServlet对应的Web上下文
2012-03-12 13:33:55 [main] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet ‘chapter2‘: initialization started
2012-03-12 13:33:55 [main] DEBUG org.springframework.web.servlet.DispatcherServlet - Servlet with name ‘chapter2‘ will try to create custom WebApplicationContext context of class ‘org.springframework.web.context.support.XmlWebApplicationContext‘, using parent context [Root WebApplicationContext: startup date [Mon Mar 12 13:33:55 CST 2012]; root of context hierarchy]
//此处使用Root WebApplicationContext作为父容器。
2012-03-12 13:33:55 [main] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace ‘chapter2-servlet‘: startup date [Mon Mar 12 13:33:55 CST 2012]; parent: Root WebApplicationContext
2012-03-12 13:33:55 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/chapter2-servlet.xml]
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader - Loading bean definitions
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.BeanDefinitionParserDelegate - Neither XML ‘id‘ nor ‘name‘ specified - using generated bean name[org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#0] //我们配置的HandlerMapping
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.BeanDefinitionParserDelegate - Neither XML ‘id‘ nor ‘name‘ specified - using generated bean name[org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter#0] //我们配置的HandlerAdapter
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.BeanDefinitionParserDelegate - Neither XML ‘id‘ nor ‘name‘ specified - using generated bean name [org.springframework.web.servlet.view.InternalResourceViewResolver#0] //我们配置的ViewResolver
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.BeanDefinitionParserDelegate - No XML ‘id‘ specified - using ‘/hello‘ as bean name and [] as aliases
//我们的处理器(HelloWorldController)
2012-03-12 13:33:55 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 4 bean definitions from location pattern [/WEB-INF/chapter2-servlet.xml]
2012-03-12 13:33:55 [main] DEBUG org.springframework.web.context.support.XmlWebApplicationContext - Bean factory for WebApplicationContext for namespace ‘chapter2-servlet‘: org.springframework.beans.factory.support.DefaultListableBeanFactory@1372656: defining beans [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#0,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,/hello]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1c05ffd
//到此容器注册的Bean初始化完毕
2012-03-12 13:33:56 [main] DEBUG org.springframework.web.servlet.DispatcherServlet - Unable to locate MultipartResolver with name ‘multipartResolver‘: no multipart request handling provided
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver‘
//默认的LocaleResolver注册
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘org.springframework.web.servlet.theme.FixedThemeResolver‘
//默认的ThemeResolver注册
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#0‘
//发现我们定义的HandlerMapping 不再使用默认的HandlerMapping。
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter#0‘
//发现我们定义的HandlerAdapter 不再使用默认的HandlerAdapter。
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver‘
//异常处理解析器ExceptionResolver
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean ‘org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver‘
2012-03-12 13:33:56 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘org.springframework.web.servlet.view.InternalResourceViewResolver#0‘
2012-03-12 13:33:56 [main] DEBUG org.springframework.web.servlet.DispatcherServlet - Published WebApplicationContext of servlet ‘chapter2‘ as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.chapter2]
//绑定FrameworkServlet初始化的Web上下文到ServletContext
2012-03-12 13:33:56 [main] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet ‘chapter2‘: initialization completed in 297 ms
2012-03-12 13:33:56 [main] DEBUG org.springframework.web.servlet.DispatcherServlet - Servlet ‘chapter2‘ configured successfully
//到此完整流程结束
从如上日志我们也可以看出,DispatcherServlet会进行一些默认的配置。接下来我们看一下默认配置吧。
3.5、DispatcherServlet默认配置
DispatcherServlet的默认配置在DispatcherServlet.properties(和DispatcherServlet类在一个包下)中,而且是当Spring配置文件中没有指定配置时使用的默认策略:
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
从如上配置可以看出DispatcherServlet在启动时会自动注册这些特殊的Bean,无需我们注册,如果我们注册了,默认的将不会注册。
因此如第二章的BeanNameUrlHandlerMapping、SimpleControllerHandlerAdapter是不需要注册的,DispatcherServlet默认会注册这两个Bean。
从DispatcherServlet.properties可以看出有许多特殊的Bean,那接下来我们就看看Spring Web MVC主要有哪些特殊的Bean。
3.6、DispatcherServlet中使用的特殊的Bean
DispatcherServlet默认使用WebApplicationContext作为上下文,因此我们来看一下该上下文中有哪些特殊的Bean:
1、Controller:处理器/页面控制器,做的是MVC中的C的事情,但控制逻辑转移到前端控制器了,用于对请求进行处理;
2、HandlerMapping:请求到处理器的映射,如果映射成功返回一个HandlerExecutionChain对象(包含一个Handler处理器(页面控制器)对象、多个HandlerInterceptor拦截器)对象;如BeanNameUrlHandlerMapping将URL与Bean名字映射,映射成功的Bean就是此处的处理器;
3、HandlerAdapter:HandlerAdapter将会把处理器包装为适配器,从而支持多种类型的处理器,即适配器设计模式的应用,从而很容易支持很多类型的处理器;如SimpleControllerHandlerAdapter将对实现了Controller接口的Bean进行适配,并且掉处理器的handleRequest方法进行功能处理;
4、ViewResolver:ViewResolver将把逻辑视图名解析为具体的View,通过这种策略模式,很容易更换其他视图技术;如InternalResourceViewResolver将逻辑视图名映射为jsp视图;
5、LocalResover:本地化解析,因为Spring支持国际化,因此LocalResover解析客户端的Locale信息从而方便进行国际化;
6、ThemeResovler:主题解析,通过它来实现一个页面多套风格,即常见的类似于软件皮肤效果;
7、MultipartResolver:文件上传解析,用于支持文件上传;
8、HandlerExceptionResolver:处理器异常解析,可以将异常映射到相应的统一错误界面,从而显示用户友好的界面(而不是给用户看到具体的错误信息);
9、RequestToViewNameTranslator:当处理器没有返回逻辑视图名等相关信息时,自动将请求URL映射为逻辑视图名;
10、FlashMapManager:用于管理FlashMap的策略接口,FlashMap用于存储一个请求的输出,当进入另一个请求时作为该请求的输入,通常用于重定向场景,后边会细述。
HttpServletBean 、 FrameworkServlet 和 DispatcherServlet 关系