Spring拦截器使用xml但不使用config类

时间:2013-04-18 11:35:42

标签: java xml spring annotations interceptor

我目前正在使用配置了xml的拦截器,如下所示:

<mvc:interceptors>
    <bean class="org.resthub.dashboard.BasicInterceptor" />
</mvc:interceptors>

但我想把它放在我的配置类中:

    @Configuration
    @ComponentScan("org.resthub.dashboard")
    @EnableWebMvc
    @EnableAspectJAutoProxy(proxyTargetClass=true)
    public class WebAppConfig extends WebMvcConfigurerAdapter{

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(new BasicInterceptor());
        }

        @Bean
        public InternalResourceViewResolver setupViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
    }

但它不起作用,我不知道为什么。永远不会调用拦截器。

有什么想法吗?

谢谢

edit:这是BasicInterceptor

    package org.resthub.dashboard;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

    public class BasicInterceptor extends HandlerInterceptorAdapter {

        @Override
        public void postHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            // TODO Auto-generated method stub
            super.postHandle(request, response, handler, modelAndView);

            System.out.println("INTERCEPTORORRR§§§§§§");
            if (modelAndView != null && modelAndView.getModelMap() != null) {
    ...
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

您的配置似乎是正确的,至少如果您已经确认它确实已被选中。但是,可能的问题是在请求期间发生了导致postHandle不执行的事情。确保在请求执行期间不会抛出任何execption。要验证Interceptor实际上是否已被使用,您可以实现preHandleafterCompletion并查看这些方法是否已执行。