SpringMVC-的作用

背景

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

标签的组成

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

    <xsd:element name="annotation-driven">
        <xsd:annotation>
            <xsd:documentation source="java:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">xsd:documentation>
        xsd:annotation>
        <xsd:complexType>
            <xsd:all minOccurs="0">
                <xsd:element name="message-converters" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>xsd:documentation>
                    xsd:annotation>
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:choice maxOccurs="unbounded">
                                <xsd:element ref="beans:bean">
                                    <xsd:annotation>
                                        <xsd:documentation>xsd:documentation>
                                    xsd:annotation>
                                xsd:element>
                                <xsd:element ref="beans:ref">
                                    <xsd:annotation>
                                        <xsd:documentation>xsd:documentation>
                                    xsd:annotation>
                                xsd:element>
                            xsd:choice>
                        xsd:sequence>
                        <xsd:attribute name="register-defaults" type="xsd:boolean" default="true">
                            <xsd:annotation>
                                <xsd:documentation>xsd:documentation>
                            xsd:annotation>
                        xsd:attribute>
                    xsd:complexType>
                xsd:element>
                <xsd:element name="argument-resolvers" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>xsd:documentation>
                    xsd:annotation>
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                <xsd:annotation>
                                    <xsd:documentation>xsd:documentation>
                                xsd:annotation>
                            xsd:element>
                        xsd:sequence>
                    xsd:complexType>
                xsd:element>          
                <xsd:element name="return-value-handlers" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>xsd:documentation>
                    xsd:annotation>
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                <xsd:annotation>
                                    <xsd:documentation>xsd:documentation>
                                xsd:annotation>
                            xsd:element>
                        xsd:sequence>
                    xsd:complexType>
                xsd:element>          
                <xsd:element name="async-support" minOccurs="0">
                    <xsd:annotation>
                        <xsd:documentation>xsd:documentation>
                    xsd:annotation>
                    <xsd:complexType>
                        <xsd:all minOccurs="0">
                            <xsd:element name="callable-interceptors" minOccurs="0">
                                <xsd:annotation>
                                    <xsd:documentation>xsd:documentation>
                                xsd:annotation>
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                            <xsd:annotation>
                                                <xsd:documentation>xsd:documentation>
                                            xsd:annotation>
                                        xsd:element>
                                    xsd:sequence>
                                xsd:complexType>
                            xsd:element>
                            <xsd:element name="deferred-result-interceptors" minOccurs="0">
                                <xsd:annotation>
                                    <xsd:documentation>xsd:documentation>
                                xsd:annotation>
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
                                            <xsd:annotation>
                                                <xsd:documentation>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">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>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">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">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">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>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>xsd:documentation>
                xsd:annotation>
            xsd:attribute>
            <xsd:attribute name="ignoreDefaultModelOnRedirect" type="xsd:boolean">
                <xsd:annotation>
                    <xsd:documentation>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这样的注解生效。不配你试试!
哈哈,好了,后面阿福会带着大家去实现自己的一些定制类,满足某些功能!
今天就到这里了!

你可能感兴趣的:(spring)