浏览 3076 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-12
最后修改:2009-03-12
介绍:接口:PropertyEditorRegistry的基本实现,负责默认编辑器和自定义编辑器的管理,主要服务于基类 BeanWrapperImpl。 重要属性: private Map defaultEditors;//装载默认编辑器 private Map customEditors;//装载自定义编辑器 private Set sharedEditors;//装载共享编辑器 private Map customEditorCache;//自定义编辑器的缓存 主要方法: 取得默认属性编辑器: public PropertyEditor getDefaultEditor(Class requiredType) { ...... return (PropertyEditor) this.defaultEditors.get(requiredType);/*取得给定属性类型的属性编辑器,defaultEditors 是以 requiredType为key ,PropertyEditor 为value的Map。这是通过下面的方法存放的。 */ } 注册一个自定义属性编辑器: public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor) { registerCustomEditor(requiredType, null, propertyEditor);/*这里null表示类型为requiredType的属性均可使用propertyEditor编辑器*/ } 注册完成在下面的方法: public void registerCustomEditor(Class requiredType, String propertyPath, PropertyEditor propertyEditor) {/*requiredType 或 propertyPath 决定那些属性使用该PropertyEditor */ if (requiredType == null && propertyPath == null) { throw new IllegalArgumentException("Either requiredType or propertyPath is required"); }/*最初需要定义一个存放自定义属性编辑器的Map*/ if (this.customEditors == null) { this.customEditors = new LinkedHashMap(16); }/*[color=red]属性名称(名称,或是嵌套路径)不空,则propertyEditor只是针对该属性注册的[/color]*/ if (propertyPath != null) { this.customEditors.put(propertyPath, new CustomEditorHolder(propertyEditor, requiredType)); this.propertySpecificEditorsRegistered = true; } else { [color=darkred]/*属性名称为空,则propertyEditor是针对属性类型为requiredType注册的*/[/color] this.customEditors.put(requiredType, propertyEditor); this.customEditorCache = null; } } 查找指定属性类型或是属性名称对应的编辑器: public PropertyEditor findCustomEditor(Class requiredType, String propertyPath) { ...... if (propertyPath != null) { if (this.propertySpecificEditorsRegistered) { /*优先查找指定属性对应的属性编辑器*/ PropertyEditor editor = getCustomEditor(propertyPath, requiredType); if (editor == null) { List strippedPaths = new LinkedList(); addStrippedPropertyPaths(strippedPaths, "", propertyPath);/*沿着属性嵌套路径查找,直到找到为止*/ for (Iterator it = strippedPaths.iterator(); it.hasNext() && editor == null;) { String strippedPath = (String) it.next(); editor = getCustomEditor(strippedPath, requiredType); } } if (editor != null) { return editor; } } if (requiredType == null) { requiredTypeToUse = getPropertyType(propertyPath); } } /* 若是没有指定属性,则找指定属性类型的对应编辑器*/ return getCustomEditor(requiredTypeToUse); } 取得属性编辑器的方法: private PropertyEditor getCustomEditor(Class requiredType) { if (requiredType == null) { return null; } /* 首先直接按属性类型去获得 */ PropertyEditor editor = (PropertyEditor) this.customEditors.get(requiredType); if (editor == null) { /* 尝试在缓冲编辑器map里获得*/ if (this.customEditorCache != null) { editor = (PropertyEditor) this.customEditorCache.get(requiredType); } if (editor == null) { /*沿着属性路径去获得(它的超类) */ for (Iterator it = this.customEditors.keySet().iterator(); it.hasNext() && editor == null;) { Object key = it.next(); if (key instanceof Class && ((Class) key).isAssignableFrom(requiredType)) { editor = (PropertyEditor) this.customEditors.get(key);/* 为每种类型缓冲编辑器,以免以后重复查找*/ if (this.customEditorCache == null) { this.customEditorCache = new HashMap(); } this.customEditorCache.put(requiredType, editor); } } } } return editor; } 判断是否包含的数组/集合元素指定的编辑器: public boolean hasCustomEditorForElement(Class elementType, String propertyPath) { if (this.customEditors == null) { return false; } if (propertyPath != null && this.propertySpecificEditorsRegistered) {/*遍历注册器属性路径,若存在与指定属性匹配的属性路径,且该属性路径对应编辑器持有者拥有元素类型对应的编辑器则返回true*/ for (Iterator it = this.customEditors.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); if (entry.getKey() instanceof String) { String regPath = (String) entry.getKey(); if (PropertyAccessorUtils.matchesProperty(regPath, propertyPath)) { CustomEditorHolder editorHolder = (CustomEditorHolder) entry.getValue(); if (editorHolder.getPropertyEditor(elementType) != null) { return true; } } } } } //若没有为该属性路径指定编辑器,则返回是否存在元素类型对应的属性编辑器 return (elementType != null && this.customEditors.containsKey(elementType)); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |