<bean id="CustomEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> 
<property name="customEditors"> 
<map> 
<entry key="java.util.Date"> 
<bean class="com.dzc.DatePropertyEditor"> </bean> 
</entry> 
</map> 
</property> 
</bean> 
packet com.dzc 
import java.beans.PropertyEditorSupport; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
public class DatePropertyEditor extends PropertyEditorSupport { @Override 
public void setAsText(String text) throws IllegalArgumentException { 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
try { 
Date d = sdf.parse(text); 
this.setValue(d); 
} catch (ParseException e) { 
e.printStackTrace(); 

} } 报错原因:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CustomEditorConfigurer' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.LinkedHashMap] to required type [java.util.Map] for property 'customEditors'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [DatePropertyEditor] to required type [java.lang.String] for property 'customEditors[java.util.Date]': no matching editors or conversion strategy found 
我看了CustomEditorConfigurer类的源码,发现customEditors属性的类型是Map <String,String>,是不是因为取出的Date类型的值无法放入Map的Value内导致的错误,而在spring2.0中运行时正常的,对比了CustomEditorConfigurer源码发现customEditors的Map类型没定义泛型。如果真是这样,那在Spring3.0中怎么写个Date类型的自定义属性编辑器啊,望各位大虾帮帮小弟,偶才刚刚开始学习spring,很多不懂,谢谢大家了。

解决方案 »

  1.   

    Spring框架的2x版本和3x(如3.0.0M3)在这个地方的定义方式是不一样的,估计你看的是2x版的资料,而使用的Spring框架是3.0M3的!
    我已经解决了这个问题,需要把定义方式改一下,即把
    <entry key="java.util.Date"> 
      <bean class="com.dzc.DatePropertyEditor"> </bean> 
    </entry> 
    改为
    <entry key="java.util.Date" value="com.dzc.DatePropertyEditor"/>
    即可搞定!
      

  2.   

    Objector说的太对了,实验成功!!环境:spring-framework-3.0.0.M4+Myeclipse7.5