码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC 指导文档解读(二)

时间:2016-10-09 16:57:23      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:

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 typeExplanation

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 [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@3af54caa]
Line 594: 2016-10-04 13:07:59 DEBUG DispatcherServlet:555 - Unable to locate ThemeResolver with name ‘themeResolver‘: using default [org.springframework.web.servlet.theme.FixedThemeResolver@1508dd9]
Line 619: 2016-10-04 13:07:59 DEBUG DispatcherServlet:692 - Unable to locate RequestToViewNameTranslator with name ‘viewNameTranslator‘: using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@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.SessionFlashMapManager@2c267a73]
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 +
                        "]");
            }
        }
    }

这便是上面的日志的原因

Spring MVC 指导文档解读(二)

标签:

原文地址:http://www.cnblogs.com/zno2/p/5930403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!