Spring MVC的handlermapping之SimpleUrlHandlerMapping初始化

前面信息同BeanNameUrlHandlerMapping,这里不再过多分析,详情请看 : Spring MVC的handlermapping之BeanNameUrlHandlerMapping初始化

同样先上类图:

 

可以看到SimpleUrlHandlerMapping是直接继承AbstractUrlHandlerMapping的

 1 public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
 2     private final Map<String, Object> urlMap = new HashMap(); //用来保存url和handler的映射关系。
 3 
 4     public SimpleUrlHandlerMapping() {
 5     }
 6 
 7     public void setMappings(Properties mappings) { //通过spring自动注入,mapping属性值
 8         CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap);
 9     }
10 
11     public void setUrlMap(Map<String, ?> urlMap) { //通过spring自动注入,urlMap的值
12         this.urlMap.putAll(urlMap);
13     }
14 
15     public Map<String, ?> getUrlMap() {
16         return this.urlMap;
17     }
18 
19     public void initApplicationContext() throws BeansException { //重写方法,自己搞定注册处理器逻辑
20         super.initApplicationContext();
21         this.registerHandlers(this.urlMap);
22     }
23 
24     protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {
25         String url;
26         Object handler;
27         if (urlMap.isEmpty()) {
28             this.logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
29         } else { //遍历map,对url和handler进行合法性修补,最后调用父类进行注册处理器
30             for(Iterator i$ = urlMap.entrySet().iterator(); i$.hasNext(); this.registerHandler(url, handler)) {
31                 Entry<String, Object> entry = (Entry)i$.next();
32                 url = (String)entry.getKey();
33                 handler = entry.getValue();
34                 if (!url.startsWith("/")) {
35                     url = "/" + url;
36                 }
37 
38                 if (handler instanceof String) {
39                     handler = ((String)handler).trim();
40                 }
41             }
42         }
43 
44     }
45 }

 

posted @ 2018-03-29 09:37  郝二驴  阅读(273)  评论(0编辑  收藏  举报