web--SpringMVC剖析

文章一直更新中......

众所周知,Spring mvc 将所有请求都在 DispatcherServlet 中处理,

我们看一看它的类层次结构:DispatcherServlet--->  FrameworkServlet-->  HttpServletBean ---> HttpServlet 

可以得知底层其实实现了HttpServlet,其实就是一个servlet
给大家翻译一下 DispatcherServlet 文档:
/**
 * Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service
 * exporters. Dispatches to registered handlers for processing a web request, providing convenient mapping and exception
 * handling facilities.
 
处理http 请求处理器和控制器的中央调度器,例如,用于web ui 控制器或者基于http的远程;调度注册的处理程序为web 请求
提供便利的映射和异常处理;
* <p>This servlet is very flexible: It can be used with just about any workflow, with the installation of the * appropriate adapter classes. It offers the following functionality that distinguishes it from other request-driven * web MVC frameworks:
这个servlet 是非常精巧的:它几乎可以被任何的工作流所使用,只要安装了合适的适配器类,它提供了如下功能使之区别其他请求驱动的web mvc 框架

* <ul> <li>It is based around a JavaBeans configuration mechanism.
它是基于java bean 的配置机制的 * * <li>It can use any {
@link HandlerMapping} implementation - pre-built or provided as part of an application - to * control the routing of requests to handler objects. Default is {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping} * and {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}. HandlerMapping objects * can be defined as beans in the servlet's application context, implementing the HandlerMapping interface, overriding * the default HandlerMapping if present. HandlerMappings can be given any bean name (they are tested by type).
它可以使用基于handlermapping 的实现,提前构建或者提供作为应用程序的一部分,来控制请求到处理对象之间的路由;默认提供了beannameurlhandlermapping
以及default annotationmapping ;处理器映射器对象可以被定义作为servlet 上下问对象的bean ;实现handlermapping 接口,覆盖默认的handlempping
handlermapping 会被赋予一个bean 名字;
* <li>It can use any {
@link HandlerAdapter}; this allows for using any handler interface. Default adapters are {@link * org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter}, {@link org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter}, * for Spring's {@link org.springframework.web.HttpRequestHandler} and {@link org.springframework.web.servlet.mvc.Controller} * interfaces, respectively. A default {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter} * will be registered as well. HandlerAdapter objects can be added as beans in the application context, overriding the * default HandlerAdapters. Like HandlerMappings, HandlerAdapters can be given any bean name (they are tested by type). 它可以使用一些处理器适配器;它允许使用任何处理接口
默认的适配器是
HttpRequestHandlerAdapter,simplecontrollerhandleradapter分别对应着 HttpRequestHandler,Controller handler接口
默认的annotationhandler也会被注册 处理器适配器对象也会在application 上下文加载进来为bean

 * <li>The dispatcher's exception resolution strategy can be specified via a {@link HandlerExceptionResolver}, for
 * example mapping certain exceptions to error pages. Default are
 * {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver},
 * {@link org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver}, and
 * {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver}. These HandlerExceptionResolvers can be overridden
 * through the application context. HandlerExceptionResolver can be given any bean name (they are tested by type).
 *
 * <li>Its view resolution strategy can be specified via a {@link ViewResolver} implementation, resolving symbolic view
 * names into View objects. Default is {@link org.springframework.web.servlet.view.InternalResourceViewResolver}.
 * ViewResolver objects can be added as beans in the application context, overriding the default ViewResolver.
 * ViewResolvers can be given any bean name (they are tested by type).
 *
 * <li>If a {@link View} or view name is not supplied by the user, then the configured {@link
 * RequestToViewNameTranslator} will translate the current request into a view name. The corresponding bean name is
 * "viewNameTranslator"; the default is {@link org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator}.
 *
 * <li>The dispatcher's strategy for resolving multipart requests is determined by a {@link
 * org.springframework.web.multipart.MultipartResolver} implementation. Implementations for Jakarta Commons FileUpload
 * and Jason Hunter's COS are included; the typical choise is {@link org.springframework.web.multipart.commons.CommonsMultipartResolver}.
 * The MultipartResolver bean name is "multipartResolver"; default is none.
 *
 * <li>Its locale resolution strategy is determined by a {@link LocaleResolver}. Out-of-the-box implementations work via
 * HTTP accept header, cookie, or session. The LocaleResolver bean name is "localeResolver"; default is {@link
 * org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver}.
 *
 * <li>Its theme resolution strategy is determined by a {@link ThemeResolver}. Implementations for a fixed theme and for
 * cookie and session storage are included. The ThemeResolver bean name is "themeResolver"; default is {@link
 * org.springframework.web.servlet.theme.FixedThemeResolver}. </ul>
 *
 * <p><b>NOTE: The {@code @RequestMapping} annotation will only be processed if a corresponding
 * {@code HandlerMapping} (for type level annotations) and/or {@code HandlerAdapter} (for method level
 * annotations) is present in the dispatcher.</b> This is the case by default. However, if you are defining custom
 * {@code HandlerMappings} or {@code HandlerAdapters}, then you need to make sure that a corresponding custom
 * {@code DefaultAnnotationHandlerMapping} and/or {@code AnnotationMethodHandlerAdapter} is defined as well -
 * provided that you intend to use {@code @RequestMapping}.
 *
 * <p><b>A web application can define any number of DispatcherServlets.</b> Each servlet will operate in its own
 * namespace, loading its own application context with mappings, handlers, etc. Only the root application context as
 * loaded by {@link org.springframework.web.context.ContextLoaderListener}, if any, will be shared.
 *
 * <p>As of Spring 3.1, {@code DispatcherServlet} may now be injected with a web
 * application context, rather than creating its own internally. This is useful in Servlet
 * 3.0+ environments, which support programmatic registration of servlet instances. See
 * {@link #DispatcherServlet(WebApplicationContext)} Javadoc for details.
 *

其实根据上一篇对servlet 详解,我们知道对于servlet 的初始化动作一定放在init 方法中,设置start 等级,这样就可以服务器启动,初始化servlet

/**
     * Map config parameters onto bean properties of this servlet, and
     * invoke subclass initialization.
     * @throws ServletException if bean properties are invalid (or required
     * properties are missing), or if subclass initialization fails.
     */
将文件配置参数映射到servlet 的bean 属性上,并调用子类初始化;
@Override public final void init() throws ServletException {

猜你喜欢

转载自www.cnblogs.com/iscys/p/9756458.html