3

I'm trying to convert a Spring project from XML to Java config and have run into the following issue with HandlerInterceptors:

XML Config (works):

<mvc:annotation-driven />
<mvc:interceptors>
    <bean class="com.mycompany.MyHandlerInterceptor" />
</mvc:interceptors>

Java Config (interceptor is never called)

@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyHandlerInterceptor());
    }

    // ...
}

According to the documentation, these two configurations should be equivalent, however in the Java config example the neither the pre or post handle methods are ever called?

What am I missing?

Thanks.

WayneC
  • 2,530
  • 3
  • 31
  • 44
  • Works fine here. Have you used your debugger or added traces in the code to make sure that your MvcConfig class is loaded and the addInterceptors method called? – JB Nizet Feb 15 '14 at 15:42
  • @JBNizet Yes. I added debug logging before/after the call to registry.addInterceptor() and they both were hit. – WayneC Feb 15 '14 at 16:23
  • Try to add @EnableWebMvc to MvcConfig class. See http://stackoverflow.com/questions/10391988/in-spring-3-1-can-mvcinterceptors-be-used-in-conjunction-with-configuration – Evgeni Dimitrov Feb 15 '14 at 17:08

1 Answers1

6

This was my own fault. I had overridden requestMappingHandlerMapping() in my MVC Java config and did not set the interceptors property on the custom HandlerMapping class.

@Bean
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    CustomRequestHandlerMapping handlerMapping = new CustomRequestHandlerMapping();
    handlerMapping.setOrder(0);
    handlerMapping.setInterceptors(getInterceptors()); // <-- This was missing
    return handlerMapping;
}
WayneC
  • 2,530
  • 3
  • 31
  • 44