spring MVC <mvc:annotation-driven>

研究SpringMvc 3.2的<mvc:annotation-driven>默认干了什么

如果不配置其他参数,大致相当于以下的配置文件(参考自org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser)

<!-- 配置路径扩展名映射的媒体类型 -->
    <bean name="pathExtensionContentNegotiationStrategy"
        class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
        <constructor-arg>
            <props>
                <!-- if romePresent -->
                <prop key="atom">application/atom+xml</prop>
                <prop key="rss">application/rss+xml</prop>
                <!-- endif -->
                <!-- if jackson2Present || jacksonPresent -->
                <prop key="json">application/json</prop>
                <!-- endif -->
                <!-- if jaxb2Present -->
                <prop key="xml">application/xml</prop>
                <!-- endif -->
            </props>
        </constructor-arg>
    </bean>  

    <!-- 配置映射媒体类型的策略 -->
    <bean name="mvcContentNegotiationManager"
        class="org.springframework.web.accept.ContentNegotiationManager">
        <constructor-arg>
            <list>
                <ref bean="pathExtensionContentNegotiationStrategy" />
            </list>
        </constructor-arg>
    </bean>  

    <!-- 配置方法级别的@RequestMapping处理器 -->
    <bean name="requestMappingHandlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="order" value="0" />
        <property name="contentNegotiationManager" ref="mvcContentNegotiationManager" />
    </bean>  

    <!-- 配置数据转换服务,默认使用格式化数据转换服务,可以对日期和数字进行格式化 -->
    <bean name="conversionService"
        class="org.springframework.format.support.DefaultFormattingConversionService">
        <constructor-arg index="0">
            <null></null>
        </constructor-arg>
        <constructor-arg index="1">
            <value>true</value>
        </constructor-arg>
    </bean>  

    <bean name="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>  

    <!-- 配置数据绑定,通过转换服务实现绑定,如果包含jsr303实现还将进行校验 -->
    <bean name="webBindingInitializer"
        class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="conversionService" ref="conversionService" />
        <!-- if jsr303Present -->
        <property name="validator" ref="validator" />
        <!-- endif -->
    </bean>  

    <bean name="byteArrayHttpMessageConverter"
        class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
    <bean name="stringHttpMessageConverter"
        class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="writeAcceptCharset" value="false" />
    </bean>
    <bean name="resourceHttpMessageConverter"
        class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
    <bean name="sourceHttpMessageConverter"
        class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
    <bean name="allEncompassingFormHttpMessageConverter"
        class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"></bean>
    <bean name="atomFeedHttpMessageConverter"
        class="org.springframework.http.converter.feed.AtomFeedHttpMessageConverter"></bean>
    <bean name="rssChannelHttpMessageConverter"
        class="org.springframework.http.converter.feed.RssChannelHttpMessageConverter"></bean>
    <bean name="jaxb2RootElementHttpMessageConverter"
        class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
    <bean name="mappingJackson2HttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
    <bean name="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>  

    <!-- 配置@RequestBody,@ResponseBody注解可用的转换器 -->
    <util:list id="messageConverters"
        list-class="org.springframework.beans.factory.support.ManagedList.ManagedList">
        <ref bean="byteArrayHttpMessageConverter" />
        <ref bean="stringHttpMessageConverter" />
        <ref bean="resourceHttpMessageConverter" />
        <ref bean="sourceHttpMessageConverter" />
        <ref bean="allEncompassingFormHttpMessageConverter" />
        <!-- if romePresent -->
        <ref bean="atomFeedHttpMessageConverter" />
        <ref bean="rssChannelHttpMessageConverter" />
        <!-- endif -->
        <!-- if jaxb2Present -->
        <ref bean="jaxb2RootElementHttpMessageConverter" />
        <!-- endif -->
        <!-- if jackson2Present -->
        <ref bean="mappingJackson2HttpMessageConverter" />
        <!-- endif -->
        <!-- if jacksonPresent -->
        <ref bean="mappingJacksonHttpMessageConverter" />
        <!-- endif -->
    </util:list>  

    <!-- 将任意类型的Controller适配为Handler -->
    <bean name="requestMappingHandlerAdapter"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="contentNegotiationManager" ref="mvcContentNegotiationManager" />
        <property name="webBindingInitializer" ref="webBindingInitializer" />
        <property name="messageConverters" ref="messageConverters" />
    </bean>  

    <!-- 这个拦截器暴露转换器服务让spring:bind和spring:eval标签可用 -->
    <bean name="csInterceptor"
        class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor">
        <constructor-arg index="0">
            <ref bean="conversionService" />
        </constructor-arg>
    </bean>  

    <!-- 现在所有拦截器都必须设定响应的路径映射 -->
    <bean name="mappedCsInterceptor"
        class="org.springframework.web.servlet.handler.MappedInterceptor">
        <constructor-arg index="0">
            <null></null>
        </constructor-arg>
        <constructor-arg index="1">
            <ref bean="csInterceptor" />
        </constructor-arg>
    </bean>  

    <!-- 使用@ExceptionHandler注解的方法来处理Exception,优先级为0(最高) -->
    <bean name="exceptionHandlerExceptionResolver"
        class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
        <property name="contentNegotiationManager" ref="mvcContentNegotiationManager" />
        <property name="messageConverters" ref="messageConverters" />
        <property name="order" value="0" />
    </bean>  

    <!-- 如果抛出的Exception类带有@ResponseStatus注解,响应返回该注解的Http状态码,优先级为1 -->
    <bean name="responseStatusExceptionResolver"
        class="org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver">
        <property name="order" value="1" />
    </bean>  

    <!-- SpringMvc内部异常处理 -->
    <bean name="defaultExceptionResolver"
        class="org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver">
        <property name="order" value="2" />
    </bean>  

spring MVC <mvc:annotation-driven>

时间: 2024-11-05 12:26:18

spring MVC <mvc:annotation-driven>的相关文章

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

Spring 3 MVC: Themes In Spring-Tutorial With Example---reference

Welcome to Part 6 of Spring 3.0 MVC Series. In previous article we saw how to add Internationalization i18n and Localization L10n support to Spring 3.0 based web application. We usedLocaleChangeInterceptor to intercept the change in locale andReloada

初试 spring web mvc

作为一名code需要了解更多的知识,编程方面的东西太多了,是个逐渐积累的过程.最近学习一下spring web mvc,写下我个人的一些经验. 1.准备jar包.spring mvc 已经到了版本4,网上的很多资料已经不在适用.给出的下载地址也无法适用.这是非常痛苦的.我费了好大功夫才获得这些JAR包. 在官方介绍文档里获得http://docs.spring.io/spring/docs/4.1.0.BUILD-SNAPSHOT/spring-framework-reference/htmls

Spring 4 MVC HelloWorld - XML示例

构建Spring MVC 4的HelloWorld实例. 使用以下技术: Spring 4.0.6.RELEASE Maven 3 JDK 1.8 如果您已经熟悉maven使用eclipse 创建项目,请随意跳过第1步. 让我们开始. 步骤1:在Eclipse中创建一个新的Maven webapp项目 Goto File-> New - > Maven Project, 取消选中"使用默认工作区位置",以防您希望在其他位置创建项目 点击下一步 从选项列表中选择maven w

Spring 4 MVC HelloWorld - 注解/JavaConfig实例

在之前的Spring 4 MVC HelloWorld - 实例中,有spring-mvc.xml和web.xml这两个xml文件.现在通过java配置,将删除这个两个xml文件. 1.pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="

Spring 4 MVC+Apache Tiles 3 Example

In this post we will integrate Apache Tiles 3 with Spring MVC 4, using annotation-based configuration. Apache Tiles is a template based, composite view framework: it allows to reuse page pieces across the application, keeping consistent look and feel

Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring boot如何快速构建一个. Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务.支持约定大于配置,目的是尽可能快地构建和运行Spring应用. 之前我们创建基于Spring的项目需要考虑添加哪些Spring依赖和第三方的依赖.使用Spring Boot后,我们可

(Spring文档翻译)Part V, the Web 17.1 Spring Web MVC framework介绍

指南文档的这个部分涵盖了Spring框架对表现层(特别是基于Web的表现层)以及WebSocket消息风格的web应用的支持. Spring框架拥有自己的web框架,Spring Web MVC,包含在前面几个章节.之后的几章是关于Spring框架对其他web技术的集成支持,像JSF等. 再之后是Spring框架的MVC porlet 框架. Spring 的MVC框架围绕着DispatcherServlet设计,DispatcherServlet将请求转发给handler,用可配置的handl

spring 3 mvc hello world + mavern +jetty

Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutorial, we show you a Spring 3 MVC hello world example, using Maven build tool. Technologies used : Spring 3.2.13.RELEASE Maven 3 JDK 1.6 Eclipse 4.4 Boos