都作了那些事情

mvc:annotation-driven是一種簡寫的配置方式,那麼mvc:annotation-driven到底作了哪些工做呢?如何替換掉mvc:annotation-driven呢?web

<mvc:annotation- driven/>在初始化的時候會自動建立兩個對 象,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 和 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter, 咱們若是想不使用<mvc:annotation-driven/>這種簡寫方式,將其替換掉的話,就必須本身手動去配置這兩個bean對 象。下面是這兩個對象的配置方法,和詳細的注視說明。spring

<?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 />了。json

相關文章
相關標籤/搜索