1.AbstractDetectingUrlHandlerMapping继承图
2.概述
加载容器所有进行url映射处理的bean
3.AbstractDetectingUrlHandlerMapping整个代码用工具翻译后的
https://blog.csdn.net/qq_39482039/article/details/119221041
4.覆盖了AbstractUrlHandlerMapping的initApplicationContext方法,注册在上下文中找到的所有处理程序
/**
* 除了*超类的初始化之外,还调用{@link #detectHandlers()}方法。
*/
@Override
public void initApplicationContext() throws ApplicationContextException {
//调用父类的initApplicationContext方法
super.initApplicationContext();
//注册在上下文中找到的所有处理程序
detectHandlers();
}
detectHandlers方法来获取上下文所有bean,留抽象方法给子类做筛选,调用父类方法将url注册成对应的处理程序
/**
* 注册在当前ApplicationContext中找到的所有处理程序。
* 处理程序的实际URL确定取决于具体的*
* {@link #determineUrlsForHandler(String)}实现。
* 不能确定此类URL的*的bean根本不被视为处理程序。
* @throws org.springframework.beans.BeansException if the handler couldn't be registered
* @see #determineUrlsForHandler(String)
*/
protected void detectHandlers() throws BeansException {
//获取到当前的上下文
ApplicationContext applicationContext = obtainApplicationContext();
//detectHandlersInAncestorContexts默认是false,获取所有的bean名称
String[] beanNames = (this.detectHandlersInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class) :
applicationContext.getBeanNamesForType(Object.class));
// 取任何我们可以确定其URL的bean名称。
for (String beanName : beanNames) {
//抽象方法子类实现,来筛选对应的bean处理程序
String[] urls = determineUrlsForHandler(beanName);
if (!ObjectUtils.isEmpty(urls)) {
//调用父类的方法,将url注册处理程序
// 找到的 URL 路径:让我们将其视为处理程序。
registerHandler(urls, beanName);
}
}
if ((logger.isDebugEnabled() && !getHandlerMap().isEmpty()) || logger.isTraceEnabled()) {
logger.debug("Detected " + getHandlerMap().size() + " mappings in " + formatMappingName());
}
}
抽象方法留给子类实现,具体处理详见BeanNameUrlHandlerMapping
https://blog.csdn.net/qq_39482039/article/details/119190086
/**
* 确定给定处理程序bean的URL。
* @param beanName the name of the candidate bean
* @return the URLs determined for the bean, or an empty array if none
*/
protected abstract String[] determineUrlsForHandler(String beanName);