SpringMVC-<mvc:annotation-driven>的作用

背景

        事实上,我们更多的时候使用这个标签只是因为我们看到人家那么配的。但是我们对其中的配置大多数时候是浑然不知的。但是,当我们想去扩展其中的一些东西的时候,才发现这个有多麽的重要!

标签的组成

        细心的同学肯定有看过这个标签的xsd定义。下面我们一起看看!

    <xsd:element name="annotation-driven">
        <xsd:annotation>
            <xsd:documentation source="java:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><![CDATA[
    Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!

    See org.springframework.web.servlet.config.annotation.EnableWebMvc Javadoc for
    information on code-based alternatives to enabling annotation-driven Spring MVC
    support.
            ]]></xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:all minOccurs="0">
                <xsd:element name="message-converters" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[
    Configures one or more HttpMessageConverter types to use for converting @RequestBody method parameters and @ResponseBody method return values.
    Using this configuration element is optional.
    HttpMessageConverter registrations provided here will take precedence over HttpMessageConverter types registered by default.
    Also see the register-defaults attribute if you want to turn off default registrations entirely.
                        ]]></xsd:documentation>
                    </xsd:annotation>
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:choice maxOccurs="unbounded">
                                <xsd:element ref="beans:bean">
                                    <xsd:annotation>
                                        <xsd:documentation><![CDATA[
        An HttpMessageConverter bean definition.
                                        ]]></xsd:documentation>
                                    </xsd:annotation>
                                </xsd:element>
                                <xsd:element ref="beans:ref">
                                    <xsd:annotation>
                                        <xsd:documentation><![CDATA[
        A reference to an HttpMessageConverter bean.
                                        ]]></xsd:documentation>
                                    </xsd:annotation>
                                </xsd:element>
                            </xsd:choice>
                        </xsd:sequence>
                        <xsd:attribute name="register-defaults" type="xsd:boolean" default="true">
                            <xsd:annotation>
                                <xsd:documentation><![CDATA[
    Whether or not default HttpMessageConverter registrations should be added in addition to the ones provided within this element.
                                ]]></xsd:documentation>
                            </xsd:annotation>
                        </xsd:attribute>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="argument-resolvers" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[
    Configures HandlerMethodArgumentResolver types to support custom controller method argument types.
    Using this option does not override the built-in support for resolving handler method arguments.
    To customize the built-in support for argument resolution configure RequestMappingHandlerAdapter directly.
                        ]]></xsd:documentation>
                    </xsd:annotation>
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                <xsd:annotation>
                                    <xsd:documentation><![CDATA[
    The HandlerMethodArgumentResolver (or WebArgumentResolver for backwards compatibility) bean definition.
                                    ]]></xsd:documentation>
                                </xsd:annotation>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>          
                <xsd:element name="return-value-handlers" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[
    Configures HandlerMethodReturnValueHandler types to support custom controller method return value handling.
    Using this option does not override the built-in support for handling return values.
    To customize the built-in support for handling return values configure RequestMappingHandlerAdapter directly.
                        ]]></xsd:documentation>
                    </xsd:annotation>
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                <xsd:annotation>
                                    <xsd:documentation><![CDATA[
    The HandlerMethodReturnValueHandler bean definition.
                                    ]]></xsd:documentation>
                                </xsd:annotation>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>          
                <xsd:element name="async-support" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[
    Configure options for asynchronous request processing.
                        ]]></xsd:documentation>
                    </xsd:annotation>
                    <xsd:complexType>
                        <xsd:all minOccurs="0">
                            <xsd:element name="callable-interceptors" minOccurs="0">
                                <xsd:annotation>
                                    <xsd:documentation><![CDATA[
    The ordered set of interceptors that intercept the lifecycle of concurrently executed
    requests, which start after a controller returns a java.util.concurrent.Callable.
                                    ]]></xsd:documentation>
                                </xsd:annotation>
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                            <xsd:annotation>
                                                <xsd:documentation><![CDATA[
    Registers a CallableProcessingInterceptor.
                                                ]]></xsd:documentation>
                                            </xsd:annotation>
                                        </xsd:element>
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                            <xsd:element name="deferred-result-interceptors" minOccurs="0">
                                <xsd:annotation>
                                    <xsd:documentation><![CDATA[
    The ordered set of interceptors that intercept the lifecycle of concurrently executed
    requests, which start after a controller returns a DeferredResult.
                                    ]]></xsd:documentation>
                                </xsd:annotation>
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                            <xsd:annotation>
                                                <xsd:documentation><![CDATA[
    Registers a DeferredResultProcessingInterceptor.
                                                ]]></xsd:documentation>
                                            </xsd:annotation>
                                        </xsd:element>
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:all>
                        <xsd:attribute name="task-executor" type="xsd:string">
                            <xsd:annotation>
                                <xsd:documentation source="java:org.springframework.core.task.AsyncTaskExecutor"><![CDATA[
    The bean name of a default AsyncTaskExecutor to use when a controller method returns a {@link Callable}.
    Controller methods can override this default on a per-request basis by returning an AsyncTask.
    By default a SimpleAsyncTaskExecutor is used which does not re-use threads and is not recommended for production.
                                ]]></xsd:documentation>
                                <xsd:appinfo>
                                    <tool:annotation kind="ref">
                                        <tool:expected-type type="java:org.springframework.core.task.AsyncTaskExecutor" />
                                    </tool:annotation>
                                </xsd:appinfo>
                            </xsd:annotation>
                        </xsd:attribute>
                        <xsd:attribute name="default-timeout" type="xsd:long">
                            <xsd:annotation>
                                <xsd:documentation><![CDATA[
    Specify the amount of time, in milliseconds, before asynchronous request handling times out.
    In Servlet 3, the timeout begins after the main request processing thread has exited and ends when the request is dispatched again for further processing of the concurrently produced result.
    If this value is not set, the default timeout of the underlying implementation is used, e.g. 10 seconds on Tomcat with Servlet 3.
                                ]]></xsd:documentation>
                            </xsd:annotation>
                        </xsd:attribute>
                    </xsd:complexType>
                </xsd:element>
            </xsd:all>
            <xsd:attribute name="conversion-service" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation source="java:org.springframework.core.convert.ConversionService"><![CDATA[
    The bean name of the ConversionService that is to be used for type conversion during field binding.
    This attribute is not required, and only needs to be specified explicitly if custom converters need to be configured.
    If not specified, a default FormattingConversionService is registered that contains converters to/from standard JDK types.
    In addition, full support for date/time formatting will be installed if the Joda Time library is present on the classpath.
                    ]]></xsd:documentation>
                    <xsd:appinfo>
                        <tool:annotation kind="ref">
                            <tool:expected-type type="java:org.springframework.core.convert.ConversionService" />
                        </tool:annotation>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="validator" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation source="java:org.springframework.validation.Validator"><![CDATA[
    The bean name of the Validator that is to be used to validate Controller model objects.
    This attribute is not required, and only needs to be specified explicitly if a custom Validator needs to be configured.
    If not specified, JSR-303 validation will be installed if a JSR-303 provider is present on the classpath.
                    ]]></xsd:documentation>
                    <xsd:appinfo>
                        <tool:annotation kind="ref">
                            <tool:expected-type type="java:org.springframework.validation.Validator" />
                        </tool:annotation>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="content-negotiation-manager" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation source="java:org.springframework.web.accept.ContentNegotiationManager"><![CDATA[
    The bean name of a ContentNegotiationManager that is to be used to determine requested media types. If not specified,
    a default ContentNegotiationManager is configured that checks the request path extension first and the "Accept" header
    second where path extensions such as ".json", ".xml", ".atom", and ".rss" are recognized if Jackson, JAXB2, or the
    Rome libraries are available. As a fallback option, the path extension is also used to perform a lookup through
    the ServletContext and the Java Activation Framework (if available).
                    ]]></xsd:documentation>
                    <xsd:appinfo>
                        <tool:annotation kind="ref">
                            <tool:expected-type type="java:org.springframework.web.accept.ContentNegotiationManager" />
                        </tool:annotation>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="message-codes-resolver" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    The bean name of a MessageCodesResolver to use to build message codes from data binding and validation error codes.
    This attribute is not required.
    If not specified the DefaultMessageCodesResolver is used.
                    ]]></xsd:documentation>
                    <xsd:appinfo>
                        <tool:annotation kind="ref">
                            <tool:expected-type type="java:org.springframework.validation.MessageCodesResolver" />
                        </tool:annotation>
                    </xsd:appinfo>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="enableMatrixVariables" type="xsd:boolean">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    Matrix variables can appear in any path segment, each matrix variable separated with a ";" (semicolon).
    For example "/cars;color=red;year=2012". By default they're removed from the URL. If this property
    is set to true, matrix variables are not removed from the URL, and the request mapping pattern
    must use URI variable in path segments where matrix variables are expected. For example "/{cars}".
    Matrix variables can then be injected into a controller method with @MatrixVariable.
                    ]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
            <xsd:attribute name="ignoreDefaultModelOnRedirect" type="xsd:boolean">
                <xsd:annotation>
                    <xsd:documentation><![CDATA[
    By default the content of the "default" model is used both during rendering and redirect scenarios. 
    Alternatively a controller method can declare a RedirectAttributes argument and use it to provide attributes for a redirect. 
    Setting this flag to true ensures the "default" model is never used in a redirect scenario even if a RedirectAttributes argument is not declared.
    Setting it to false means the "default" model may be used in a redirect if the controller method doesn't declare a RedirectAttributes argument.
    The default setting is false but new applications should consider setting it to true.
                    ]]></xsd:documentation>
                </xsd:annotation>
            </xsd:attribute>
        </xsd:complexType>
    </xsd:element>

        仔细去找的话大家会发现这个标签里面允许出现如下的四种内置标签:

  1. message-converters
  2. argument-resolvers
  3. async-support
  4. return-value-handlers

        实际上这四种标签是各有各的用处,我们来看一个简单的例子。


<mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <value>*/*</value>
                </property>
            </bean>
        </mvc:message-converters>

        大家仔细看,这里定义了一个bean,然后这个bean的作用实际上就是将我们的controller的出参转换为json的。大家看这个bean的名字就能够明白了。好了,这其实是当我们的方法上配置了@ResponseBody注解的时候才会生效。
也就是一个自定义的转换类。
        当然,我们自己也能写这样的东西。

简单说明

        上面我们说到了四种的内置标签,1是用来做出参转换的。2呢,是入参转换,3是异步支持,4,阿福也不太清楚。
        这里需要的bean都是可以自定义的,当然,spring为我们留了各种各样的接口,就是为了让我们去更多的定制自己的功能。一个好的框架大抵就是这幅模样吧!

总结

        其实就算大家不再这个标签内配置任何东西,也是需要的。他就是为了使@Controller这样的注解生效。不配你试试!
哈哈,好了,后面阿福会带着大家去实现自己的一些定制类,满足某些功能!
今天就到这里了!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值