Spring MVC 指导文档解读(二)

Special Bean Types In the WebApplicationContext

解读

1.WebApplicationContext 特有的几种 Bean Types

2. 也表明 与之相对的 还有 ApplicationContext

下面这几种特有的 web bean types 也理应定义到 web 的上下文配置文件中,比如 a-servlet.xml

Table 22.1. Special bean types in the WebApplicationContext

Bean type Explanation

HandlerMapping


Maps incoming requests to handlers and a list of pre- and post-processors (handler interceptors) based on some criteria the details of which vary by HandlerMapping implementation. The most popular implementation supports annotated controllers but other implementations exists as well.


HandlerAdapter


Helps the DispatcherServlet to invoke a handler mapped to a request regardless of the handler is actually invoked. For example, invoking an annotated controller requires resolving various annotations. Thus the main purpose of aHandlerAdapter is to shield the DispatcherServlet from such details.


HandlerExceptionResolver


Maps exceptions to views also allowing for more complex exception handling code.


ViewResolver


Resolves logical String-based view names to actual View types.


LocaleResolver &LocaleContextResolver


Resolves the locale a client is using and possibly their time zone, in order to be able to offer internationalized views


ThemeResolver


Resolves themes your web application can use, for example, to offer personalized layouts


MultipartResolver


Parses multi-part requests for example to support processing file uploads from HTML forms.


FlashMapManager


Stores and retrieves the "input" and the "output" FlashMap that can be used to pass attributes from one request to another, usually across a redirect.

查看dispatcherServlet 相关日志

Line 2: 2016-10-04 13:07:57 DEBUG DispatcherServlet:118 - Initializing servlet ‘dispatcher‘
Line 9: 2016-10-04 13:07:57 INFO  DispatcherServlet:489 - FrameworkServlet ‘dispatcher‘: initialization started
Line 10: 2016-10-04 13:07:57 DEBUG DispatcherServlet:617 - Servlet with name ‘dispatcher‘ will try to create custom WebApplicationContext context of class ‘org.springframework.web.context.support.XmlWebApplicationContext‘, using parent context [null]
Line 577: 2016-10-04 13:07:59 DEBUG DispatcherServlet:511 - Unable to locate MultipartResolver with name ‘multipartResolver‘: no multipart request handling provided
Line 586: 2016-10-04 13:07:59 DEBUG DispatcherServlet:533 - Unable to locate LocaleResolver with name ‘localeResolver‘: using default [[email protected]3af54caa]
Line 594: 2016-10-04 13:07:59 DEBUG DispatcherServlet:555 - Unable to locate ThemeResolver with name ‘themeResolver‘: using default [[email protected]]
Line 619: 2016-10-04 13:07:59 DEBUG DispatcherServlet:692 - Unable to locate RequestToViewNameTranslator with name ‘viewNameTranslator‘: using default [org.spri[email protected]4a233666]
Line 629: 2016-10-04 13:07:59 DEBUG DispatcherServlet:753 - Unable to locate FlashMapManager with name ‘flashMapManager‘: using default [org.springframework.web.servlet.support.S[email protected]]
Line 643: 2016-10-04 13:07:59 DEBUG DispatcherServlet:568 - Published WebApplicationContext of servlet ‘dispatcher‘ as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher]
Line 644: 2016-10-04 13:07:59 INFO  DispatcherServlet:508 - FrameworkServlet ‘dispatcher‘: initialization completed in 1538 ms
Line 645: 2016-10-04 13:07:59 DEBUG DispatcherServlet:139 - Servlet ‘dispatcher‘ configured successfully

dispatcherServlet 干了什么

1. 设置默认的策略(位于DispatcherServlet.properties 中)

    static {
        // Load default strategy implementations from properties file.
        // This is currently strictly internal and not meant to be customized
        // by application developers.
        try {
            ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
            defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load ‘DispatcherServlet.properties‘: " + ex.getMessage());
        }
    }

# 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

DispatcherServlet.properties

2. 初始化

    protected void initStrategies(ApplicationContext context) {
        initMultipartResolver(context);
        initLocaleResolver(context);
        initThemeResolver(context);
        initHandlerMappings(context);
        initHandlerAdapters(context);
        initHandlerExceptionResolvers(context);
        initRequestToViewNameTranslator(context);
        initViewResolvers(context);
    }

举例来说就是存在指定的就用指定的,不存在指定的就用默认的

    private void initRequestToViewNameTranslator(ApplicationContext context) {
        try {
            this.viewNameTranslator =
                    context.getBean(REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, RequestToViewNameTranslator.class);
            if (logger.isDebugEnabled()) {
                logger.debug("Using RequestToViewNameTranslator [" + this.viewNameTranslator + "]");
            }
        }
        catch (NoSuchBeanDefinitionException ex) {
            // We need to use the default.
            this.viewNameTranslator = getDefaultStrategy(context, RequestToViewNameTranslator.class);
            if (logger.isDebugEnabled()) {
                logger.debug("Unable to locate RequestToViewNameTranslator with name ‘" +
                        REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME + "‘: using default [" + this.viewNameTranslator +
                        "]");
            }
        }
    }

这便是上面的日志的原因

时间: 2024-10-08 09:46:57

Spring MVC 指导文档解读(二)的相关文章

Spring MVC 指导文档解读(一)

22.1 指导文档章节 In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. 解读 一. DispatcherServlet 可以定义多个 二. root WebApplicationContext 适用于所有的 Dispat

Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探

Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便写下这篇博客心得 Google API:http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#terms 其实大家要学习Android的技术,Google的API就是最详细的指导书了,而且通俗易懂,就算看不懂

Swagger UI教程 API 文档神器 搭配Node使用 web api 接口文档 mvc接口文档

两种方案 一.Swagger 配置 web Api 接口文档美化 二.通过NodeJS 发布Swagger UI 配置api 文档 先说一下简单的 Swagger 配置 web Api  Swagger-UI本身只提供在线测试功能,要集成它还需要告诉它本项目提供的各种服务和参数信息.这里就需要一些工作量了,不过好在许多第三方库已经给我们完成了这一工作.我这里用的是Swashbuckle,使用它也比较简单,直接使用Nuget添加其程序包即可: 1.初始化包  PM> Install-Package

myeclipse中关联spring doc帮助文档

这是一篇分享技巧的文章:使用myeclipse关联帮助文档 ① 选中spring.jar ② 鼠标右击,选择properties,弹出框中选择Javadoc Location,找到对应的文档位置,OK ③ 选择spring.jar中的类 ④ 按住F1,弹出一个help框,选择Java help:Javadoc for 'xxx' ⑤ 下图就是对应的文档内容 myeclipse中关联spring doc帮助文档,码迷,mamicode.com

Spring MVC 官方文档

Spring MVC 官方文档地址:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc Spring MVC 是基于Servlet API构建的Web框架. 原文地址:https://www.cnblogs.com/smailjunk/p/11174845.html

五年阿里摸爬滚打,写出这一份Spring boot实践文档

毋庸置疑,Spring Boot在众多从事Java微服务开发的程序员群体中是一个很特别的存在.说它特别是因为它确实简化了基于Spring技术栈的应用/微服务开发过程,使得我们能够很快速地就搭建起一个应用的脚手架并在其上进行项目的开发,再也不用像以前那样使用大量的XML或是注解了,应用在这样的约定优于配置的前提下可以以最快的速度创建出来. 今天就给大家分享五年阿里摸爬滚打,写出的这一份Spring boot实践文档 如果你需要的话可以点赞后[点击我]来获取到 基础应用开发(入门) 1.Spring

MQ-ONS:ONS 开发指导文档

ylbtech-MQ-ONS:ONS 开发指导文档 关键字:消息队列.ONS.日志.控制台.阿里云MVP 1.返回顶部 1. 本文由曹素杰(阿里云 MVP 石化盈科信息技术有限责任公司 高级工程师)提供,若使用请标明出处.1. ONS介绍支持半事务特性的消息队列2. ONS流程图 3. ONS注意事项:a) 消息发送注意事项: 事务放到send的回调方法中 Event对象要尽量的小,上限64k,消息的内容越多网络延迟越大 回调方法要注意超时时间的设置 打印日志b) 消息消费注意事项: 方法保证幂

关于Spring配置文件xml文档的schema约束

最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. [html] view plain copy print? <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:m

Spring中xml文档的schema约束

最开始使用Spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springfram