Search code examples
spring-mvcconfigurationspring-3spring-annotations

In Spring 3.1 can <mvc:interceptors> be used in conjunction with @Configuration


I am migration from Spring 3.0.5 to 3.1 since I need to have custom RequestMappingHandlerMapping. I am facing problems in plug-in of extended RequestMappingHandlerMapping - I had existing servlet-conetxt.xml and I added WebConfig with @Configuration annotation. But, I always get error ambiguos mapping (since new annotation defined in ExtendedRequestMappingHandlerMapping is not takign in effect).

I have various levels of interceptors defined in servlet-context.xml which I want to keep in XML configuration. I want to use .

Is there a way to use conjunction of servlet-context.xml and at the same time extend RequestMappingHandlerMapping. If this has to be done using @COnfiguration - can I use both @COnfiguration and servlet-context.xml? Any help would be appreciated as I have been trying this since a long time.

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>com.test.config</param-value>
</context-param>

Solution

  • Yes, you can use it: Example:

    @EnableWebMvc
    @Configuration
    public class WebConfig extends WebMvcConfigurerAdapter {
    
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocalInterceptor());
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
     }
    
    }
    

    just refer to

    http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-interceptors for more details.