如何根据http请求选择合适的controller是MVC中一项十分关键的功能,在Spring MVC中,HandlerMapping接口是这一活动的抽象。同时,Spring MVC也提供了一系列HandlerMapping的实现,根据一定的规则选择controller。如果当前的HandlerMappign实现中没有能够满足你所需要的规则是,可以通过实现HandlerMapping接口进行扩展。下面,就来看一下HandlerMapping相关的类图:
在Spring MVC中,关于HandlerMapping的使用,主要包括两个部分:注册和查找。在HandlerMapping的实现中,持有一个handlerMap这样一个HashMap<String, Object>,其中key是http请求的path信息,value可以是一个字符串,或者是一个处理请求的HandlerExecutionChain,如果是String类型,则会将其视为Spring的bean名称。在HandlerMapping对象的创建中,IoC容器执行了一个容器回调方法setApplicationContext,在这个方法中调用initApplicationContext方法进行初始化,各个子类可以根据需求的不同覆写这个方法。关于handlerMap信息的注册就是在initApplicationContext方法中被执行的。下面就来看一下注册url到controller映射信息的实现:
SimpleUrlHandlerMapping中的注册实现代码:
protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {
if (urlMap.isEmpty()) {
logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
}
else {
//urlMap信息是根据配置文件注入进来的
for (Map.Entry<String, Object> entry : urlMap.entrySet()) {
String url = entry.getKey();
Object handler = entry.getValue();
// Prepend with slash if not already present.
if (!url.startsWith("/")) {
url = "/" + url;
}
// Remove whitespace from handler bean name.
if (handler instanceof String) {
handler = ((String) handler).trim();
}
registerHandler(url, handler);
}
}
}
AbstractDetectingUrlHandlerMapping中的注册实现代码:
protected void detectHandlers() throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("Looking for URL mappings in application context: " + getApplicationContext());
}
String[] beanNames = (this.detectHandlersInAncestorContexts ?
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
getApplicationContext().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");
}
}
}
}
单个的映射关系注册是在registerHandler方法中执行的:
protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException {
Assert.notNull(urlPath, "URL path must not be null");
Assert.notNull(handler, "Handler object must not be null");
Object resolvedHandler = handler;
// Eagerly resolve handler if referencing singleton via name.
//如果给定的handeler是字符串,则认为是bean name,直接到IoC容器中取得bean instance
if (!this.lazyInitHandlers && handler instanceof String) {
String handlerName = (String) handler;
if (getApplicationContext().isSingleton(handlerName)) {
resolvedHandler = getApplicationContext().getBean(handlerName);
}
}
Object mappedHandler = this.handlerMap.get(urlPath);
//同一path不能对应多个处理对象
if (mappedHandler != null) {
if (mappedHandler != resolvedHandler) {
throw new IllegalStateException(
"Cannot map handler [" + handler + "] to URL path [" + urlPath +
"]: There is already handler [" + resolvedHandler + "] mapped.");
}
}
else {
if (urlPath.equals("/")) {
if (logger.isInfoEnabled()) {
logger.info("Root mapping to handler [" + resolvedHandler + "]");
}
setRootHandler(resolvedHandler);
}
else if (urlPath.equals("/*")) {
if (logger.isInfoEnabled()) {
logger.info("Default mapping to handler [" + resolvedHandler + "]");
}
setDefaultHandler(resolvedHandler);
}
else {
//将path和handler放在handlerMap中,保存了它们之间的映射关系
this.handlerMap.put(urlPath, resolvedHandler);
if (logger.isInfoEnabled()) {
logger.info("Mapped URL path [" + urlPath + "] onto handler [" + resolvedHandler + "]");
}
}
}
}
通过以上代码,在HandlerMapping对象被创建的时候,即完成了url到handler之间的映射关系的注册。handlerMap已经被赋值,可以被使用了。
在完成url到handler映射关系的注册后,就可以使用getHandler方法,根据http请求获得handler对象了。下面,首先看一下getHandler方法的调用时序:
下面就主要关注一下上图中涉及到的方法的代码实现。
public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
Object handler = getHandlerInternal(request);
//如果没有找到匹配的handler,则使用默认handler
if (handler == null) {
handler = getDefaultHandler();
}
if (handler == null) {
return null;
}
// Bean name or resolved handler?
//如果给定的handler是字符串类型,则认为是bean name
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
//如果Handler是HandlerExecutionChain类型,则只是添加拦截器,否则会创建一个新的HandlerExecutionChain
return getHandlerExecutionChain(handler, request);
}
将url信息与handler进行匹配查找的操作是在lookupHandler方法中执行的,下面就来看一下lookupHandler方法的实现:
//这个方法可能的返回值是HandlerExecutionChain对象或者是null
//在HandlerExecutionChain对象中的handler,是根据handlerMap中取出来的bean name获得到的bean instance
protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {
// Direct match?
//直接匹配
Object handler = this.handlerMap.get(urlPath);
if (handler != null) {
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
//从IoC容器中取出handler
handler = getApplicationContext().getBean(handlerName);
}
validateHandler(handler, request);
//创建一个HandlerExecutionChain对象并返回
return buildPathExposingHandler(handler, urlPath, urlPath, null);
}
// Pattern match?
//根据一定的模式匹配规则
List<String> matchingPatterns = new ArrayList<String>();
for (String registeredPattern : this.handlerMap.keySet()) {
if (getPathMatcher().match(registeredPattern, urlPath)) {
matchingPatterns.add(registeredPattern);
}
}
String bestPatternMatch = null;
if (!matchingPatterns.isEmpty()) {
Collections.sort(matchingPatterns, getPathMatcher().getPatternComparator(urlPath));
if (logger.isDebugEnabled()) {
logger.debug("Matching patterns for request [" + urlPath + "] are " + matchingPatterns);
}
bestPatternMatch = matchingPatterns.get(0);
}
if (bestPatternMatch != null) {
//处理最佳匹配
handler = this.handlerMap.get(bestPatternMatch);
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
validateHandler(handler, request);
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPatternMatch, urlPath);
Map<String, String> uriTemplateVariables =
getPathMatcher().extractUriTemplateVariables(bestPatternMatch, urlPath);
//返回一个HandlerExecutionChain对象
return buildPathExposingHandler(handler, bestPatternMatch, pathWithinMapping, uriTemplateVariables);
}
// No handler found...
return null;
}
最后在关注一下getHandlerInternal的代码实现
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
//查找符合匹配规则的handler。可能的结果是HandlerExecutionChain对象或者是null
Object handler = lookupHandler(lookupPath, request);
//如果没有找到匹配的handler,则需要处理下default handler
if (handler == null) {
// We need to care for the default handler directly, since we need to
// expose the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE for it as well.
Object rawHandler = null;
if ("/".equals(lookupPath)) {
rawHandler = getRootHandler();
}
if (rawHandler == null) {
rawHandler = getDefaultHandler();
}
//在getRootHandler和getDefaultHandler方法中,可能持有的是bean name。
if (rawHandler != null) {
// Bean name or resolved handler?
if (rawHandler instanceof String) {
String handlerName = (String) rawHandler;
rawHandler = getApplicationContext().getBean(handlerName);
}
validateHandler(rawHandler, request);
handler = buildPathExposingHandler(rawHandler, lookupPath, lookupPath, null);
}
}
//如果handler还是为空,则抛出错误。
if (handler != null && this.mappedInterceptors != null) {
Set<HandlerInterceptor> mappedInterceptors =
this.mappedInterceptors.getInterceptors(lookupPath, this.pathMatcher);
if (!mappedInterceptors.isEmpty()) {
HandlerExecutionChain chain;
if (handler instanceof HandlerExecutionChain) {
chain = (HandlerExecutionChain) handler;
} else {
chain = new HandlerExecutionChain(handler);
}
chain.addInterceptors(mappedInterceptors.toArray(new HandlerInterceptor[mappedInterceptors.size()]));
}
}
if (handler != null && logger.isDebugEnabled()) {
logger.debug("Mapping [" + lookupPath + "] to handler '" + handler + "'");
}
else if (handler == null && logger.isTraceEnabled()) {
logger.trace("No handler mapping found for [" + lookupPath + "]");
}
return handler;
}
经过上述代码的处理,就可以使用getHandler方法获得到HandlerExecutionChain对象了。DispatcherServlet获得到HandlerExecutionChain对象后,便可以获得真正的handler,调用相应的Controller了。
这篇文章主要包括两个方面的内容:HandlerMapping中url到handler映射关系的注册和根据url查找handler。第一部分是在HandlerMapping对象创建后,使用IoC容器的回调方法触发的,第二部分是在DispatchServlet中,调用getHandler方法触发的。