问题描述
<beanid="CustomEditorConfigurer"class="org.springframework.beans.factory.config.CustomEditorConfigurer"><propertyname="customEditors"><map><entrykey="java.util.Date"><beanclass="com.dzc.DatePropertyEditor"></bean></entry></map></property></bean>packetcom.dzcimportjava.beans.PropertyEditorSupport;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;publicclassDatePropertyEditorextendsPropertyEditorSupport{@OverridepublicvoidsetAsText(Stringtext)throwsIllegalArgumentException{SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");try{Dated=sdf.parse(text);this.setValue(d);}catch(ParseExceptione){e.printStackTrace();}}}报错原因:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'CustomEditorConfigurer'definedinclasspathresource[applicationContext.xml]:Initializationofbeanfailed;nestedexceptionisorg.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[java.util.LinkedHashMap]torequiredtype[java.util.Map]forproperty'customEditors';nestedexceptionisjava.lang.IllegalArgumentException:Cannotconvertvalueoftype[DatePropertyEditor]torequiredtype[java.lang.String]forproperty'customEditors[java.util.Date]':nomatchingeditorsorconversionstrategyfound我看了CustomEditorConfigurer类的源码,发现customEditors属性的类型是Map<String,String>,是不是因为取出的Date类型的值无法放入Map的Value内导致的错误,而在spring2.0中运行时正常的,对比了CustomEditorConfigurer源码发现customEditors的Map类型没定义泛型。如果真是这样,那在Spring3.0中怎么写个Date类型的自定义属性编辑器啊,望各位大虾帮帮小弟,偶才刚刚开始学习spring,很多不懂,谢谢大家了。
解决方案
解决方案二:
顶一个,我最近也遇到这个问题