Boz*_*zho 18 java spring spring-mvc
应该很容易:
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptor" />
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
但这样就不会调用拦截器.
ska*_*man 29
默认情况下,Spring将注册a BeanNameUrlHandlerMapping和a DefaultAnnotationHandlerMapping,而不需要任何显式配置.
如果您定义自己的HandlerMappingbean,那么默认的bean将不会被注册,您只需获得明确声明的bean.
到现在为止还挺好.
添加<mvc:annotation-driven/>到混音时会出现问题.这也声明了它自己的DefaultAnnotationHandlerMapping,它取代了默认值.但是,如果您也声明自己的那个,那么最终会有两个.由于它们是按照声明的顺序查询的,这通常意味着<mvc:annotation-driven/>首先被注册的人被调用,而你自己的被忽略.
如果DefaultAnnotationHandlerMapping注册的<mvc:annotation-driven/>行为与默认行为相同,即如果明确声明的行为优先,那就更好了,但这不是他们编写它的方式.
我目前的偏好是根本不使用<mvc:annotation-driven/>,它太混乱了,并且在与其他配置选项混合时太不可预测.它并没有真正做任何特别复杂的事情,明确添加它为你做的事情并不困难或冗长,最终结果更容易理解.
小智 13
我遇到的问题:Spring MVC标记与DefaultAnnotationHandlerMapping的自定义定义不相符.
为什么..?原因在上面的答案中得到了很好的解释.
为什么我想使用DefaultAnnotationHandlerMapping?我想为我的每个请求定义一个拦截器.用于确定USER AGENT..mobile或浏览器的Spring-Mobile拦截器?
现在由于mvc-annotation和DefaultAnnotationHandlerMapping的这种冲突,我不能再使用DefaultAnnotationHandlerMapping了.问题归结为我如何用标签注册我的拦截器.
解决方案很简单......但很难找到.发布它以便对其他解决方案寻求者有所帮助.使用标记并在dispathcer-servlet.xml 示例中注册拦截器bean :
<mvc:interceptors>
<!-- This runs for all mappings -->
<bean class="main.com.XXX.MobileDeviceResolverHanlderInterceptor"/>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)
此行为的原因是org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping应用程序上下文中存在两个类型的bean .Spring解决了这两个问题,但只问第一个拦截器.要解决此问题,应将以下init参数设置为DispatcherServlet
<init-param>
<param-name>detectAllHandlerMappings</param-name>
<param-value>false</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
这使得调度程序servlet仅使用中handlerMapping定义的x-servlet.xml
除此之外,为什么这是默认行为.我期待着春天社区的回答.
在我的情况下,我无法摆脱,<mvc:annotation-driven/>因为我使用杰克逊使用注释的json支持.
我尝试了什么,将我的所有拦截器移动<mvc:interceptors>到单独的"xml"文件(interceptor-config.xml)中并从我的x-dispatcher-servlet.xml导入它
<import resource="interceptor-config.xml"/>
Run Code Online (Sandbox Code Playgroud)
它解决了我的问题,并避免默认的'DefaultAnnotationHandlerMapping'bean我的应用程序上下文.
您可以直接在'x-dispatcher-servlet.xml'中复制/粘贴拦截器内容,而不是创建单独的'xml'.
以下是我的拦截器:
<mvc:interceptors>
<mvc:interceptor>
<!-- Intercepting specific URL -->
<mvc:mapping path="/abc/**" />
<bean id= "myInterceptor"
class="xx.xxx.xxx.MyInterceptor" />
</mvc:interceptor>
<mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)