一、RequestMappingHandlerMapping RequestMappingHandlerAdapter与mvc:annotation-driven的区别
在配置中经常出现RequestMappingHandlerMapping,这个是对注解@RequestMapping的处理。其目的是帮助DispatcherServlet找到对应处理的Controller来处理响应的请求。
mvc:annotaion-driven(处理类:org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser)包含的注解处理更广,除了@RequestMapping的处理,还包括
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
异常处理:
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
mvc:annotation-driven的作用:主要是用来帮助我们处理请求映射,决定是哪个controller的哪个方法来处理当前请求,以及异常处理。
二、annotaion-driven与RequestMappingHandlerMapping 混合使用
由于annotaion-driven中存在RequestMappingHandlerMapping 如果想要自定义的RequestMappingHandlerMappingXX生效需要覆盖掉annotaion-driven中的RequestMappingHandlerMapping
如何实现呢?
class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
//需要覆盖
public static final String HANDLER_MAPPING_BEAN_NAME = RequestMappingHandlerMapping.class.getName();
public static final String HANDLER_ADAPTER_BEAN_NAME = RequestMappingHandlerAdapter.class.getName();