2 hkxutao HKxutao 于 2014.08.26 17:11 提问

spring4.0.6 注入Date类型时,属性编辑器无效

这是MyDate类
package org.xjs.pojo;

import java.util.Date;

public class MyDate {
private Date date;

public Date getDate() {
    return this.date;
}

public void setDate(Date date) {
    this.date = date;
}

}
这是自定义属性编辑器类
package org.xjs.exec;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DatePropertyEditor extends PropertyEditorSupport {
private String format;

@Override
public void setAsText(String arg0) throws IllegalArgumentException {
    SimpleDateFormat sdf = new SimpleDateFormat(this.format);
    try {
        this.setValue(sdf.parse(arg0));
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

public String getFormat() {
    return this.format;
}

public void setFormat(String format) {
    this.format = format;
}

}
这是bean配置文件
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="customEditors">
        <map>
            <entry key="java.util.Date">
                <bean class="org.xjs.exec.DatePropertyEditor">
                    <property name="format" value="yyyy-MM-dd" />
                </bean>
            </entry>
        </map>
    </property>
</bean>

<bean id="myDate" class="org.xjs.pojo.MyDate">
    <property name="date" value="2013-05-09" />
</bean>


这是测试方法
package org.xjs.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xjs.pojo.MyDate;

public class Main {
private static ApplicationContext applicationContext;

public static void main(String[] args) {
    applicationContext = new ClassPathXmlApplicationContext("simple.xml");
    MyDate myDate = applicationContext.getBean("myDate", MyDate.class);
    System.out.println(myDate.getDate().toString());
}

}

以下是报错
八月 26, 2014 5:07:48 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7f36a: startup date [Tue Aug 26 17:07:48 CST 2014]; root of context hierarchy
八月 26, 2014 5:07:48 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [simple.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.beans.factory.config.CustomEditorConfigurer#0' defined in class path resource [simple.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 [org.xjs.exec.DatePropertyEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.xjs.exec.DatePropertyEditor]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:167)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at org.xjs.main.Main.main(Main.java:11)
Caused by: 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 [org.xjs.exec.DatePropertyEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.xjs.exec.DatePropertyEditor]
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:479)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1461)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
... 11 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [org.xjs.exec.DatePropertyEditor] to required type [java.lang.Class] for property 'customEditors[java.util.Date]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.xjs.exec.DatePropertyEditor]
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:263)
at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:623)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:208)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459)
... 17 more

9个回答

expleeve
expleeve   2015.12.08 20:23

我正好遇到了这个问题,发现这篇帖子,没有得到帮助信息,看了下源码,应该是CustomEditorConfigurer中的customEditors需要的value类型为Class导致的,
我尝试使用

```



org.xjs.exec.DatePropertyEditor



可以完成执行,但这样不能无法配置Bean的属性,暂时没有找到太好的方法。
看你的发布时间已经很早了,这里留言希望抛砖引玉把
expleeve
expleeve   2015.12.08 20:25

我真的很不喜欢CSDN的这个破编辑器。。。

org.xjs.exec.DatePropertyEditor


expleeve
expleeve   2015.12.08 20:26


org.xjs.exec.DatePropertyEditor

还不让删除?

expleeve
expleeve   2015.12.08 20:26


org.xjs.exec.DatePropertyEditor

还不让删除?

expleeve
expleeve   2015.12.08 20:27

这是什么狗屁网站啊!!!

songkai558919
songkai558919   2017.09.19 11:10

配置改成这样




fate_killer_liu_jie
fate_killer_liu_jie   2018.01.18 15:49

public void setCustomEditors(Map, Class<? extends PropertyEditor>> customEditors) {
this.customEditors = customEditors;
}

这玩意改了,注入的时候不能用bean,要用类名
fate_killer_liu_jie
fate_killer_liu_jie   2018.01.18 15:53

书是3.2的,4.0以上要这样配置







fate_killer_liu_jie
fate_killer_liu_jie   2018.01.18 15:53
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date" value="lcf.context.DatePropertyEditor">
                </entry>
            </map>
        </property>
    </bean>
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!