拦截器与冲突解决

当不配置<mvc:annotation-driven> 时:

 

<!-- 自动扫描com.yunos.controller下的@controller注解的类 -->

<context:component-scan base-package="com.yunos.controller" />

 

<bean id="logInterceptor" class="com.yunos.interceptor.LogInterceptor" />

 

<!-- 为controller增加拦截器logInterceptor -->

<bean

class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<property name="interceptors">

<list>

<ref bean="logInterceptor" />

</list>

</property>

</bean>

 

<!-- 重写AnnotationMethodHandlerAdapter,为controller方法增加入参user -->

<bean

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="customArgumentResolvers">

<list>

<bean class="com.yunos.resolver.UserArgumentResolver"></bean>

</list>

</property>

</bean>

 

当xml文件中使用了<mvc:annotation-driven />时,需修改为如下定义,否则,拦截器失效:

<!-- 自动扫描com.yunos.controller下的@controller注解的类 -->

<context:component-scan base-package="com.yunos.controller" />

<!-- 当使用<mvc:annotation-driven />时,要用下面这种方法声明拦截器 -->

<mvc:interceptors>

<mvc:interceptor>

<mvc:mapping path="/**" />

<bean class="com.yunos.interceptor.LogInterceptor" />

</mvc:interceptor>

</mvc:interceptors>

<!-- 当使用<mvc:annotation-driven />时,要用下面这种方法声明拦截器AnnotationMethodHandlerAdapter -->

<mvc:annotation-driven>

<mvc:argument-resolvers>

<bean class="com.yunos.resolver.UserArgumentResolver" />

</mvc:argument-resolvers>

</mvc:annotation-driven>

<!-- 使用validate验证需要声明 -->

<mvc:annotation-driven />

你可能感兴趣的:(annotation)