基本流程图,方便查看
initThemeResolver
这个跟前面讲initLocaleResolver
一样,最后获取默认的。
initHandlerMappings
这个获取处理器映射器就不一样啦,因为可能有多个,首先去容器里取HandlerMapping
类型的多个实例,这个是取的到的,因为自动配置中配置了一些,如果没取到才会去找默认的:
public static final String HANDLER_MAPPING_BEAN_NAME = "handlerMapping";
private void initHandlerMappings(ApplicationContext context) {
this.handlerMappings = null;
if (this.detectAllHandlerMappings) {
//可以找多个
// Find all HandlerMappings in the ApplicationContext, including ancestor contexts.
Map<String, HandlerMapping> matchingBeans =
BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
if (!matchingBeans.isEmpty()) {
this.handlerMappings = new ArrayList<>(matchingBeans.values());
// We keep HandlerMappings in sorted order.
AnnotationAwareOrderComparator.sort(this.handlerMappings);
}
}
else {
//只能找一个
try {
HandlerMapping hm = context.getBean(HANDLER_MAPPING_BEAN_NAME, HandlerMapping.class);
this.handlerMappings = Collections.singletonList(hm);
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore, we'll add a default HandlerMapping later.
}
}
// Ensure we have at least one HandlerMapping, by registering
// a default HandlerMapping if no other mappings are found.
if (this.handlerMappings == null) {
this.handlerMappings = getDefaultStrategies(context, HandlerMapping.class);
if (logger.isTraceEnabled()) {
logger.trace("No HandlerMappings declared for servlet '" + getServletName() +
"': using default strategies from DispatcherServlet.properties");
}
}
}
最后拿到了5
个:
都是哪来的
我们看看这些是怎么来的,其实都是自动配置里的EnableWebMvcConfiguration
中的。
WebMvcAutoConfiguration
的EnableWebMvcConfiguration
中RequestMappingHandlerMapping
:
WebMvcAutoConfiguration
的EnableWebMvcConfiguration
中WelcomePageHandlerMapping
:
WebMvcAutoConfiguration
的EnableWebMvcConfiguration
的父类WebMvcConfigurationSupport
中BeanNameUrlHandlerMapping
:
WebMvcAutoConfiguration
的EnableWebMvcConfiguration
的父类WebMvcConfigurationSupport
中RouterFunctionMapping
:
WebMvcAutoConfiguration
的EnableWebMvcConfiguration
的父类WebMvcConfigurationSupport
中SimpleUrlHandlerMapping
:
好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵。