1. 程式人生 > ><mvc:annotation-driven>註冊了什麽

<mvc:annotation-driven>註冊了什麽

span tor pre provides register aop and tco obj

前言

上一篇文章dispatcherservlet初始化中提到,如果沒有配置handlermapping就會采取默認的策略進行配置handlermapping,這篇文章就要講述mvc:annotation-driven對默認策略的改變。它背後到底註冊了哪些HandlerMapping和HandlerAdapter。

1. 配置mvc:annotation-driven

在dispatcherservlet對應的配置文件上有這樣一段配置

    <mvc:annotation-driven />
    
    <aop:aspectj-autoproxy proxy-target-class
="true"/> <mvc:interceptors> <ref bean="performanceLogInterceptor"></ref> <ref bean="concurrentCtrlInterceptor"></ref> <ref bean="authenticationInterceptor"></ref> <ref bean="authorityInterceptor"></ref> <ref bean="tpsControlInterceptor"></ref> </mvc:interceptors>

開啟了<mvc:annotation-driven/>,然後我們會找到這樣的一個實現類AnnotationDrivenBeanDefinitionParser解析處理這個標簽

/**
*這裏清清楚楚寫著該類是專門處理 <mvc:annotation-driven/>標簽的
* A {@link BeanDefinitionParser} that provides the configuration for the * {@code <annotation-driven/>} MVC namespace element
*
*這裏說明了註冊的HandlerMapping * <p>This class registers the following {
@link HandlerMapping}s:</p> * <ul> * <li>{@link RequestMappingHandlerMapping} * ordered at 0 for mapping requests to annotated controller methods. * <li>{@link BeanNameUrlHandlerMapping} * ordered at 2 to map URL paths to controller bean names. * </ul> …… */ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser { …… }

我們來看看mvc:annotation-driven都註冊了哪些東西

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        Object source = parserContext.extractSource(element);
        XmlReaderContext readerContext = parserContext.getReaderContext();

        RootBeanDefinition handlerMappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);

        ManagedList<?> messageConverters = getMessageConverters(element, source, parserContext);

        RootBeanDefinition handlerAdapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        

          ……

        RootBeanDefinition exceptionHandlerExceptionResolver = new RootBeanDefinition(ExceptionHandlerExceptionResolver.class);
      

        RootBeanDefinition responseStatusExceptionResolver = new RootBeanDefinition(ResponseStatusExceptionResolver.class);
        

        RootBeanDefinition defaultExceptionResolver = new RootBeanDefinition(DefaultHandlerExceptionResolver.class);
   // Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
        MvcNamespaceUtils.registerDefaultComponents(parserContext, source);

        parserContext.popAndRegisterContainingComponent();

        return null;
    }

MvcNamespaceUtils.registerDefaultComponents的內容如下:

public static void registerDefaultComponents(ParserContext parserContext, Object source) {
        registerBeanNameUrlHandlerMapping(parserContext, source);
        registerHttpRequestHandlerAdapter(parserContext, source);
        registerSimpleControllerHandlerAdapter(parserContext, source);
    }

至此所註冊的HandlerMapping和HandlerAdapter我們都找到了。

<mvc:annotation-driven>註冊了什麽