Spring MVC的handlermapping之SimpleUrlHandlerMapping初始化

时间:2021-10-28 14:33:24

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

同样先上类图:

Spring MVC的handlermapping之SimpleUrlHandlerMapping初始化

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

 public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
private final Map<String, Object> urlMap = new HashMap(); //用来保存url和handler的映射关系。 public SimpleUrlHandlerMapping() {
} public void setMappings(Properties mappings) { //通过spring自动注入,mapping属性值
CollectionUtils.mergePropertiesIntoMap(mappings, this.urlMap);
} public void setUrlMap(Map<String, ?> urlMap) { //通过spring自动注入,urlMap的值
this.urlMap.putAll(urlMap);
} public Map<String, ?> getUrlMap() {
return this.urlMap;
} public void initApplicationContext() throws BeansException { //重写方法,自己搞定注册处理器逻辑
super.initApplicationContext();
this.registerHandlers(this.urlMap);
} protected void registerHandlers(Map<String, Object> urlMap) throws BeansException {
String url;
Object handler;
if (urlMap.isEmpty()) {
this.logger.warn("Neither 'urlMap' nor 'mappings' set on SimpleUrlHandlerMapping");
} else { //遍历map,对url和handler进行合法性修补,最后调用父类进行注册处理器
for(Iterator i$ = urlMap.entrySet().iterator(); i$.hasNext(); this.registerHandler(url, handler)) {
Entry<String, Object> entry = (Entry)i$.next();
url = (String)entry.getKey();
handler = entry.getValue();
if (!url.startsWith("/")) {
url = "/" + url;
} if (handler instanceof String) {
handler = ((String)handler).trim();
}
}
} }
}