3

在一个库项目(使用 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 应用程序中,我只在 servlet 配置中包含 libservletcfg.xml。

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;
    }

}
4

4 回答 4

2

检查您是否有另一个libservletcfg.xml闲逛。classpath*:将匹配所有这些。

于 2013-10-11T19:28:59.653 回答
1

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

于 2018-08-03T05:33:00.033 回答
1

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

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

于 2019-11-18T03:56:59.077 回答
0

您需要使用 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

链接中找到并验证了自己

希望这可以帮助 !!

于 2018-10-20T14:25:28.693 回答