springMVC源码分析--SimpleUrlHandlerMapping(四)

来源:互联网 发布:买黄金投资软件 编辑:程序博客网 时间:2024/06/08 08:55

上一篇博客springMVC源码分析--AbstractUrlHandlerMapping(三)中我们介绍了AbstractUrlHandlerMapping,主要介绍了一个handlerMap的url和Handler的关系存取的过程。



在上一博客中我们介绍了handlerMap有一个注册url和Handler关系的注册函数,这个函数的调用是在实现类SimpleUrlHandlerMapping中实现的,目的是springMVC容器启动时将url和handler的对应关系注册到handlerMap中。

SimpleUrlHandlerMapping有一个初始化容器上下问的操作,调用父类的super.initApplicationContext会将bean注入到容器中,registerHandlers将url和Handler的对应关系注册到urlMap中,容器初始化之后会调用setMappings或者setUrlMap将url和handler的对应关系注册的urlMap中。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public void setMappings(Properties mappings) {  
  2.         CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap);  
  3.     }  
  4.   
  5.     public void setUrlMap(Map<String, ?> urlMap) {  
  6.         this.urlMap.putAll(urlMap);  
  7.     }  

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //重写父类的方法,注册到父类的map中  
  2.     @Override  
  3.     public void initApplicationContext() throws BeansException {  
  4.         super.initApplicationContext();  
  5.         registerHandlers(this.urlMap);  
  6.     }  
registerHandlers的实现如下,会调用上一篇博客中我们介绍的registerHandler函数,完成url和handler的关系注册。

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //将所有的url和Handler的对应关系放到父类AbstractURLHandlerMapping的map中  
  2.     protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {  
  3.         if (urlMap.isEmpty()) {  
  4.             logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");  
  5.         }  
  6.         else {  
  7.             for (Map.Entry<String, Object> entry : urlMap.entrySet()) {  
  8.                 String url = entry.getKey();  
  9.                 Object handler = entry.getValue();  
  10.                 // Prepend with slash if not already present.  
  11.                 if (!url.startsWith("/")) {  
  12.                     url = "/" + url;  
  13.                 }  
  14.                 // Remove whitespace from handler bean name.  
  15.                 if (handler instanceof String) {  
  16.                     handler = ((String) handler).trim();  
  17.                 }  
  18.                 registerHandler(url, handler);  
  19.             }  
  20.         }  
  21.     }  
越是到了实现子类,子类需要完成的功能越少,SimpleUrlHandlerMapping完成的工作就是容器初始化时获取所有的url和handler的对应关系,将url和handler的关系注册到handlerMap中即可,它的使命就完成了。

SimpleUrlHandlerMapping完整代码如下:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {  
  2.   
  3.     private final Map<String, Object> urlMap = new HashMap<String, Object>();  
  4.   
  5.   
  6.       
  7.     public void setMappings(Properties mappings) {  
  8.         CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap);  
  9.     }  
  10.   
  11.     public void setUrlMap(Map<String, ?> urlMap) {  
  12.         this.urlMap.putAll(urlMap);  
  13.     }  
  14.   
  15.       
  16.     public Map<String, ?> getUrlMap() {  
  17.         return this.urlMap;  
  18.     }  
  19.   
  20.   
  21.     //重写父类的方法,注册到父类的map中  
  22.     @Override  
  23.     public void initApplicationContext() throws BeansException {  
  24.         super.initApplicationContext();  
  25.         registerHandlers(this.urlMap);  
  26.     }  
  27.       
  28.     //将所有的url和Handler的对应关系放到父类AbstractURLHandlerMapping的map中  
  29.     protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {  
  30.         if (urlMap.isEmpty()) {  
  31.             logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");  
  32.         }  
  33.         else {  
  34.             for (Map.Entry<String, Object> entry : urlMap.entrySet()) {  
  35.                 String url = entry.getKey();  
  36.                 Object handler = entry.getValue();  
  37.                 // Prepend with slash if not already present.  
  38.                 if (!url.startsWith("/")) {  
  39.                     url = "/" + url;  
  40.                 }  
  41.                 // Remove whitespace from handler bean name.  
  42.                 if (handler instanceof String) {  
  43.                     handler = ((String) handler).trim();  
  44.                 }  
  45.                 registerHandler(url, handler);  
  46.             }  
  47.         }  
  48.     }  
  49.   
  50. }
0 0
原创粉丝点击