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

喜欢而已 提交于 2019-12-18 13:39:07

问题


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>

回答1:


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.




回答2:


if use

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
  @Autowired
  Anything anything;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
     log.info(anything.toString());//this will exception,how to fix?
    registry.addInterceptor(new LocalInterceptor());
    registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
 }

}

the @service can not be setting to Interceptor



来源:https://stackoverflow.com/questions/10391988/in-spring-3-1-can-mvcinterceptors-be-used-in-conjunction-with-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!