技术分享 SPRING SPRING MVC 查看内容

mvc:annotation-driven

老高 | 发布于 2016-11-01 14:19| 浏览()| 评论() | 收藏() | 点赞() | 打印

摘要: mvc:annotation-driven是一种简写的配置方式,那么mvc:annotation-driven到底做了哪些工作呢?如何替换掉mvc:annotation-driven呢?

mvc:annotation-driven是一种简写的配置方式,那么mvc:annotation-driven到底做了哪些工作呢?如何替换掉mvc:annotation-driven呢?

<mvc:annotation- driven/>在初始化的时候会自动创建两个对 象,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 和 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 我们如果想不使用<mvc:annotation-driven/>这种简写方式,将其替换掉的话,就必须自己手动去配置这两个bean对 象。下面是这两个对象的配置方法,和详细的注视说明。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="useDefaultSuffixPattern" value="false" />
        <property name="interceptors">
            <list>
                <bean class="org.mspring.mlog.web.interceptor.RememberMeInterceptor" />
                <bean class="org.mspring.mlog.web.interceptor.SettingInterceptor" />
                <bean class="org.mspring.platform.web.query.interceptor.QueryParameterInterceptor" />
            </list>
        </property>
    </bean>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <-- 这个converter是我自己定义的,是为了解决spring自带的StringHttpMessageConverter中文乱码问题的 -->
                <bean class="org.mspring.platform.web.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8" />
                </bean>
                <!-- 这里可以根据自己的需要添加converter -->
                <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
                <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
                <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
                <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
                <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
                <!-- -->
            </list>
        </property>
        <property name="customArgumentResolvers">
            <list>
                <bean class="org.mspring.platform.web.resolver.UrlVariableMethodArgumentResolver" />
            </list>
        </property>

        <property name="webBindingInitializer">
            <bean id="webBindingInitializer"
                class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="conversionService" ref="conversionService" />
            </bean>
        </property>
    </bean>

    <!-- 1, 注册ConversionService -->
    <bean id="conversionService"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="org.mspring.platform.web.converter.DateConverter">
                    <property name="pattern" value="yyyy-MM-dd HH:mm:ss" />
                </bean>
            </list>
        </property>
        <property name="formatters">
            <list>
                <bean class="org.mspring.mlog.web.formatter.factory.TagFormatAnnotationFormatterFactory"></bean>
                <bean class="org.mspring.mlog.web.formatter.factory.EncodingFormatAnnotationFormatterFactory"></bean>
            </list>
        </property>
    </bean>

</beans>


上一篇: 没有了
下一篇: SpringMVC中使用Interceptor拦截器

发表评论(对文章涉及的知识点还有疑问,可以在这里留言,老高看到后会及时回复的。)

表情