Java 如何使用 spring mvc 3.0 注册处理程序拦截器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3230633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:36:56  来源:igfitidea点击:

How to register handler interceptors with spring mvc 3.0?

javaspringspring-mvc

提问by Bozho

It should be easy:

这应该很容易:

<bean id="handlerMapping"
   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="myInterceptor" />
        </list>
    </property>
</bean>

but this way the interceptor isn't called.

但这样拦截器不会被调用。

采纳答案by skaffman

By default, Spring will register a BeanNameUrlHandlerMapping, and a DefaultAnnotationHandlerMapping, without any explicit config required.

默认情况下,Spring 将注册 aBeanNameUrlHandlerMapping和 a DefaultAnnotationHandlerMapping,无需任何显式配置。

If you define your own HandlerMappingbeans, then the default ones will not be registered, and you'll just get the explicitly declared ones.

如果您定义自己的HandlerMappingbean,则不会注册默认 bean,您只会获得显式声明的 bean。

So far, so good.

到现在为止还挺好。

The problem comes when you add <mvc:annotation-driven/>to the mix. This alsodeclares its own DefaultAnnotationHandlerMapping, which replaces the defaults. However, if you also declare your own one, then you end up with two. Since they are consulted in order of declaration, this usually means the one registered by <mvc:annotation-driven/>gets called first, and your own one gets ignored.

当您添加<mvc:annotation-driven/>到混合物中时,问题就来了。这声明了它自己的DefaultAnnotationHandlerMapping,它替换了默认值。但是,如果您还声明自己的一个,那么您最终会得到两个。由于它们是按照声明的顺序进行咨询的,这通常意味着<mvc:annotation-driven/>首先调用由注册的那个,而您自己的一个被忽略。

It would be better if the DefaultAnnotationHandlerMappingregistered by <mvc:annotation-driven/>acted like the default one, i.e. if explicitly declared ones took precedence, but that's not the way they wrote it.

如果DefaultAnnotationHandlerMapping注册的<mvc:annotation-driven/>行为像默认的那样会更好,即如果明确声明的人优先,但这不是他们编写的方式。

My current preference is to not use <mvc:annotation-driven/>at all, it's too confusing, and too unpredictable when mixed with other config options. It doesn't really do anything especially complex, it's not difficult or verbose to explicitly add the stuff that it does for you, and the end result is easier to follow.

我目前的偏好是根本不使用<mvc:annotation-driven/>,它太混乱,与其他配置选项混合时太不可预测。它并没有真正做任何特别复杂的事情,明确添加它为您所做的东西并不困难或冗长,最终结果更容易遵循。

回答by Bozho

The reason for this behaviour is that two beans of type org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMappingexist in the application context. Spring resolves the two, but asks only the first for interceptors. To fix this, the following init parameter should be set to the DispatcherServlet

这种行为的原因是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>

This makes the dispatcher servlet use only the handlerMappingdefined in the x-servlet.xml

这使得调度的servlet仅使用handlerMapping在定义x-servlet.xml

It is beyond me why this is the default behaviour. I'm expecting an answer from the spring community.

我无法理解为什么这是默认行为。我期待着春天社区的回答。

回答by axtavt

In Spring MVC 3.0 you can use <mvc:interceptors>instead of manual defining the handler mapping.

在 Spring MVC 3.0 中,您可以使用<mvc:interceptors>而不是手动定义处理程序映射。

回答by Swapnil Vargaonkar

Problem I faced: Spring MVC tag doesn't go well with custom definition of DefaultAnnotationHandlerMapping.

我面临的问题:Spring MVC 标记与 DefaultAnnotationHandlerMapping 的自定义定义不兼容。

Why..? the reason is very well explained in the answers above.

为什么..?原因在上面的答案中得到了很好的解释。

Why i wanted to use DefaultAnnotationHandlerMapping? I want to define an interceptor for my every request. a Spring-Mobile interceptor to determine the USER AGENT..mobile or a browser?

为什么我想使用 DefaultAnnotationHandlerMapping?我想为我的每个请求定义一个拦截器。一个 Spring-Mobile 拦截器来确定 USER AGENT..mobile 还是浏览器?

Now Due to this clash of mvc-annotationand DefaultAnnotationHandlerMapping, I cant use DefaultAnnotationHandlerMapping anymore. The problem comes down to how can i register my interceptors with tag.

现在由于mvc-annotationDefaultAnnotationHandlerMapping 的冲突,我不能再使用 DefaultAnnotationHandlerMapping 了。问题归结为如何使用标签注册我的拦截器。

The solutionwas simple...but hard to find. Posting it so it can be helpful to the other solution seekers.. Use tag and register the interceptor bean in your dispathcer-servlet.xml example:

解决方案很简单……但很难找到。发布它,以便它可以对其他解决方案寻求者有所帮助.. 使用标记并在您的 dispathcer-servlet.xml示例中注册拦截器 bean :

<mvc:interceptors>
 <!-- This runs for all mappings -->
     <bean  class="main.com.XXX.MobileDeviceResolverHanlderInterceptor"/>
</mvc:interceptors>

回答by arviarya

In my case I can NOT get rid of <mvc:annotation-driven/>as I am using Hymanson for json support using annotation.

在我的情况下,我无法摆脱,<mvc:annotation-driven/>因为我使用 Hymanson 使用注释进行 json 支持。

What I tried, moved my all interceptors <mvc:interceptors>in separate "xml" file (interceptor-config.xml) and imported it from my x-dispatcher-servlet.xml

我尝试过,将所有拦截器移动<mvc:interceptors>到单独的“xml”文件(interceptor-config.xml)中,并从我的 x-dispatcher-servlet.xml 中导入它

<import resource="interceptor-config.xml"/>

It solve my issue and avoid default 'DefaultAnnotationHandlerMapping' beans my application context.

它解决了我的问题并避免了我的应用程序上下文的默认“DefaultAnnotationHandlerMapping”bean。

Rather than creating separate 'xml', you can copy/paste interceptor contents directly in 'x-dispatcher-servlet.xml'.

您可以直接在“x-dispatcher-servlet.xml”中复制/粘贴拦截器内容,而不是创建单独的“xml”。

Following is my interceptor:

以下是我的拦截器:

  <mvc:interceptors> 
         <mvc:interceptor>
             <!-- Intercepting specific URL -->
             <mvc:mapping path="/abc/**" />
             <bean id= "myInterceptor" 
                 class="xx.xxx.xxx.MyInterceptor" />
         </mvc:interceptor>
<mvc:interceptors>