目录
  1. 配置文件
  2. BeanNameUrlHandlerMapping类
  3. AbstractDetectingUrlHandlerMapping类
  4. 总结
springMVC(5) HandlerMapping实现之BeanNameUrlHandlerMapping

这一次我们来看一下HandlerMapping的另一种实现方式BeanNameUrlHandlerMapping。

配置文件

1
2
3
4
5
<bean name="/hello.htm" class="com.raistudies.ui.comtroller.HelloController"/>

<bean name="/sayHello*" class="com.raistudies.ui.comtroller.HelloController"/>

<bean id="urlHandler" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

好像完全看不出来”/hello.htm”和”/sayHello*”使用了BeanNameUrlHandlerMapping,因为它们之间直接都是分离的,但为什么这样可以调用BeanNameUrlHandlerMapping,我们看源码来理解。

BeanNameUrlHandlerMapping类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class BeanNameUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping {

/**
* Checks name and aliases of the given bean for URLs, starting with "/".
*/
@Override
protected String[] determineUrlsForHandler(String beanName) {
List<String> urls = new ArrayList<>();
if (beanName.startsWith("/")) {
urls.add(beanName);
}
String[] aliases = obtainApplicationContext().getAliases(beanName);
for (String alias : aliases) {
if (alias.startsWith("/")) {
urls.add(alias);
}
}
return StringUtils.toStringArray(urls);
}

}

BeanNameUrlHandlerMapping类中只有一个determineUrlsForHandler,这个方法很短,所做的功能就是找到beanName所对应的urls,简单点理解urls应该就是本名+别名。

不过有个细节我们需要注意,那就是BeanNameUrlHandlerMapping只会处理那些前缀为”/“的urls。

接下来看BeanNameUrlHandlerMapping的父类。

AbstractDetectingUrlHandlerMapping类

首先从initApplicationContext()看起

1
2
3
4
public void initApplicationContext() throws ApplicationContextException {
super.initApplicationContext();
detectHandlers();
}

里面调用了detectHandlers()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected void detectHandlers() throws BeansException {
ApplicationContext applicationContext = obtainApplicationContext();
if (logger.isDebugEnabled()) {
logger.debug("Looking for URL mappings in application context: " + applicationContext);
}
String[] beanNames = (this.detectHandlersInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class) :
applicationContext.getBeanNamesForType(Object.class));

// Take any bean name that we can determine URLs for.
for (String beanName : beanNames) {
String[] urls = determineUrlsForHandler(beanName);
if (!ObjectUtils.isEmpty(urls)) {
// URL paths found: Let's consider it a handler.
registerHandler(urls, beanName);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Rejected bean name '" + beanName + "': no URL paths identified");
}
}
}
}

看到下面这行代码

1
2
3
String[] beanNames = (this.detectHandlersInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class) :
applicationContext.getBeanNamesForType(Object.class));

我们发现原来AbstractDetectingUrlHandlerMapping将配置文件中所有的bean都扫描了出来,因为Java中所有类都继承Object。这也就解释了最初的配置文件部分的疑问。

那么剩下的逻辑就很简单了,就是将所有的以”/“开头的url与其对应handler进行绑定。

至于绑定逻辑和上一篇文章中所讲的SimpleUrlHandlerMapping一样了,因为它们都继承了同一个父类AbstractUrlHandlerMapping。

总结

从源码中我们可以理解BeanNameUrlHandlerMapping作为默认的HandlerMapping的原因,因为BeanNameUrlHandlerMapping扫描了配置文件中所有的bean。

注意事项:只有name开头为”/“的bean才会被最终映射。

文章作者: 谷河
文章链接: https://www.lyytaw.com/spring/springMVC(5)-HandlerMapping%E5%AE%9E%E7%8E%B0%E4%B9%8BBeanNameUrlHandlerMapping/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 谷河|BLOG