handlerMapping和handlerAdapter前面几篇文章或多或少都已经提及,此篇文章一方面是为了加深印象,另一方面通过部分源码再进一步了解深入。
protected void registerHandlers(Map urlMap) throws BeansException {
if (urlMap.isEmpty()) {
logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
}
else {
//urlMap信息是根据配置文件注入进来的
for (Map.Entry 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");
}
}
}
}
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已经被赋值,可以被使用了。
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);
}
//这个方法可能的返回值是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 matchingPatterns = new ArrayList();
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 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 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。
projectController
managementController
userController
fileUploadController
detectAllHandlerMappings
false
此时spring mvc将查找名 为“handlerMapping”的bean并作为当前系统中唯一的handlermapping。如果没有定义handlerMapping的话,则spring mvc将按照org.springframework.web.servlet.DispatcherServlet所在目录下的DispatcherServlet.properties中所定义的org.springframework.web.servlet.HandlerMapping的内容来加载缺省的handlerMapping(用户没有自定义Strategies的情况下)。
detectAllHandlerAdapters
false
来强制系统只加载bean name 为” handlerAdapter”handlerAdapter。加载不到的情况下将按照DispatcherServlet.properties中所定义的org.springframework.web.servlet.HandlerAdapter的内容来加载缺省的handlerAdapter。