在DispacthServlet类中的initHandlerMappings(context)前,RequestMappingHandlerMapping已经被初始化了;这个过程是怎么进行的呢;其实这个过程是在解析DispatchServlet的过程中,刷新IoC容器时初始化的;
刷新IoC容器在这个调用中触发,实例化单例
// Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory);
List<String> beanNames = new ArrayList<String(this.beanDefinitionNames);
for (String beanName : beanNames) {
//循环生成(non-lazy-init)单例
}
List里的第一个就是RequestMappingHandlerMapping;
后面就是getBean操作;由于RequestMappingHandlerMapping间接的实现ApplicationContextAware
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
这里是把当前IoC容器给RequestMappingHanlderMapping让其可以去容器里查找映射;
经过一系列调用,然后是AbstractHandlerMethodMapping,initHandlerMethods方法进行方法扫描
/**
* Scan beans in the ApplicationContext, detect and register handler methods.
* @see #isHandler(Class)
* @see #getMappingForMethod(Method, Class)
* @see #handlerMethodsInitialized(Map)
*/
protected void initHandlerMethods() {
if (logger.isDebugEnabled()) {
logger.debug("Looking for request mappings in application context: " + getApplicationContext());
}
String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().getBeanNamesForType(Object.class));
//循环找出映射
for (String beanName : beanNames) {
if (isHandler(getApplicationContext().getType(beanName))){
detectHandlerMethods(beanName);
}
}
handlerMethodsInitialized(getHandlerMethods());
}
扫描类;并查找映射信息;其中一个重要的类是RequestMappingHandlerMapping。中一个查找方法;
/**
* Uses method and type-level @{@link RequestMapping} annotations to create
* the RequestMappingInfo.
* @return the created RequestMappingInfo, or {@code null} if the method
* does not have a {@code @RequestMapping} annotation.
* @see #getCustomMethodCondition(Method)
* @see #getCustomTypeCondition(Class)
*/
@Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo info = null;
//查看方法名上是否有@RequestMapping
RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (methodAnnotation != null) {
//获取@RequestMapping上的信息
RequestCondition<?> methodCondition = getCustomMethodCondition(method);
info = createRequestMappingInfo(methodAnnotation, methodCondition);
//获取类名上@RequestMapping的信息
RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
if (typeAnnotation != null) {
RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
}
}
return info;
}