【问题标题】:Spring HandlerInterceptor called twiceSpring HandlerInterceptor 调用了两次
【发布时间】:2013-10-11 10:34:43
【问题描述】:

在一个库项目中(使用 Spring 3.2.4)我定义了多个拦截器。 servlet 配置 xml 文件包含在要导入 Web 应用程序的 jar 中。拦截器用于多个 servlet xml,因为它们应用于具有不同拦截器的不同 Dispatcher servlet。

问题是,拦截器被调用了两次,但处理程序(控制器)只被调用了一次。

拦截器在库项目中定义:

public class SomeInterceptor extends HandlerInterceptorAdapter {

        @Override
        public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
            System.out.println("afterCompletionCalled");
        }

        @Override
        public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
            System.out.println("preHandle called");
            return true;
        }

        @Override
        public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception {
        }
    }

servlet 配置以 jar 文件的形式提供,随后包含在应用程序中。

libservletcfg.xml:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.example.controller" annotation-config="true" />

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
    </bean>

    <mvc:annotation-driven />

   <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean class="com.example.SomeInterceptor"/>
        </mvc:interceptor>    
    </mvc:interceptors>

</beans>

在 Web 应用程序中,我只是将 libservletcfg.xml 包含在 servlet 配置中。

servletConfig.xml:

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <import resource="classpath*:libservletcfg.xml"/>

    <context:component-scan base-package="com.example.app.controller" annotation-config="true" />

</beans>

这个 servletConfig.xml 用作 web.xml 中 Dispatcher servlet 的上下文配置:

<servlet>
    <servlet-name>someServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:servletConfig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>someServlet</servlet-name>
    <url-pattern>/someUrl/*</url-pattern>
</servlet-mapping>

谁能告诉我为什么拦截器被调用两次或配置有任何问题(可能导致意外行为)?

编辑

两次调用的控制器示例:

@Controller
@RequestMapping(value = "/someUrl")
public class SampleController {

    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody SampleResponseBody sampleMethod(@RequestBody final SampleRequestBody pSampleRequestBody) {
        final SampleResponseBody response = new SampleResponseBody();
        return response;
    }

}

【问题讨论】:

  • 贴一个处理方法的例子,拦截器被调用了两次。
  • 在问题中添加了一个示例。
  • 尝试调试并检查您的HandlerInterceptor 的两个实例是否已注册。
  • 做到了。 HandlerInterceptor 有两个不同的实例。到达断点的顺序:Instance1.preHandle(...), Instance2.preHandle(...), Handler, Instance2.postHandle(...), Instance1.postHandle(...)

标签: java spring spring-mvc


【解决方案1】:

我也面临同样的问题,拦截器被调用了两次。 问题出在根配置类的@ComponentScan 声明中,创建了两个实例,一个在 Web 上下文中,另一个在根上下文中。

因此,从根上下文中删除拦截器包后问题得到修复。

【讨论】:

    【解决方案2】:

    我们遇到了同样的拦截器被调用两次的问题,这是因为我们在 web.xml 以及 @Configuration java 文件中定义了 applicationContext.xml。

    【讨论】:

    • 您可能是对的,但由于该项目前段时间已关闭。我仍然会将您的回答标记为正确,因为这很可能是原因。
    • 我不认为,情况是这样,因为我只标记了一次,并且根据这个答案 (stackoverflow.com/questions/26995395/…) 它意味着发生。
    【解决方案3】:

    检查您周围没有其他 libservletcfg.xmlclasspath*: 将匹配所有这些。

    【讨论】:

    • 事实并非如此。我搜索了IDE中的所有配置文件,甚至在提取的war-file中搜索了libservletcfg.xml的出现,我只找到了一个。
    【解决方案4】:

    你需要使用 org.springframework.web.servlet.AsyncHandlerInterceptor :

    public interface AsyncHandlerInterceptor extends HandlerInterceptor {
    
        void afterConcurrentHandlingStarted(
                HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception;
    
    }
    

    Spring MVC 执行序列:

    preHandle
    afterConcurrentHandlingStarted
    preHandle
    postHandle
    afterCompletion
    

    link找到并验证了自己

    希望对你有帮助!!

    【讨论】:

    • 您的链接失效了,能否更新一下?
    猜你喜欢
    • 1970-01-01
    • 2015-01-15
    • 2016-07-19
    • 2015-11-18
    • 2016-03-03
    • 2017-06-25
    • 2013-01-21
    • 2012-09-08
    • 2016-04-27
    相关资源
    最近更新 更多