7

我正在自学 Spring 4.0.0 M3 以下是代码,

package org.chebus.springs;

import java.util.Date;

public class Traingle {
    private String name;
    private int height;
    private Date date;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Date getDate() {
        return date;
    }

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

    public void drawShape() {
        System.out.println(getName() + " Traingle height " + getHeight()
                + " Date = " + getDate());
    }

}

主要的

ApplicationContext ctx = new ClassPathXmlApplicationContext("org/chebus/springs/Spring.xml");
        Traingle traingle = ctx.getBean("traingle",Traingle.class);
        traingle.drawShape();

XML 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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-2.5.xsd">

<bean id = "traingle" class="org.chebus.springs.Traingle">
  <property name="name" value = "RightAngled"/>
  <property name="height" value = "20"/>
  <property name="date" value = "2013-09-10"/>
</bean>

<bean id="dateEditor"
        class="org.springframework.beans.propertyeditors.CustomDateEditor">

        <constructor-arg>
            <bean class="java.text.SimpleDateFormat">
                <constructor-arg value="yyyy-MM-dd" />
            </bean>
        </constructor-arg>
        <constructor-arg value="true" />

    </bean>

    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date">
                    <ref local="dateEditor" />
                </entry>
            </map>
        </property>
    </bean>


</beans>

例外:

java.lang.IllegalArgumentException:无法将类型 [org.springframework.beans.propertyeditors.CustomDateEditor] 的值转换为属性“customEditors [java.util.Date]”所需的类型 [java.lang.Class]:PropertyEditor [org.springframework .beans.propertyeditors.ClassEditor] 在 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:260) 在 org.springframework.beans.TypeConverterDelegate.convertToTypedMap (TypeConverterDelegate.java:620) 在 org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:205) 在 org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) ... 还有 17 个

不知道我哪里出错了。感谢你的帮助。谢谢!

4

2 回答 2

24

很好,这似乎是 Spring 4.0+ 的新行为,您的代码可以与 Spring 的 3.2.x 版本一起干净地工作。

原因似乎是因为customEditorsin 的类型CustomEditorConfigurer随着 Spring 4.0+ 而改变。虽然它是Map<String, ?>Spring 3.2.x 的类型,但它是Map<Class<?>, Class<? extends PropertyEditor>>Spring 4.0+ 的类型。

解决方法是改为创建一个自定义 PropertyEditorRegistrar,这样:

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;

public class CustomDateEditorRegistrar implements PropertyEditorRegistrar {

     @Override
     public void registerCustomEditors(PropertyEditorRegistry registry) {
         registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
     }
}

并在配置中使用它:

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
    <property name="propertyEditorRegistrars">
        <list>
            <bean class="dateeditor.CustomDateEditorRegistrar"/>
        </list>
    </property>
</bean>
于 2013-09-10T01:13:19.730 回答
0

在 spring 中默认没有内置的 Date 值。给你: 如何在 Spring xml 配置文件中初始化 Java Date 对象?

于 2013-09-10T00:04:25.970 回答