都做了那些事情


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>
View Code

這樣,我們就可以就成功的替換掉<mvc:annotation-driven />了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM