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.propertyeditors.CustomDateEditor] 型の不適切な値を org.springframework.beans.TypeConverterDelegate.convertToTypedMap で返しました(TypeConverterDelegate.java:620) at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:205) at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459) ... 17 もっと見る

どこが間違っているのかわかりません。あなたの助けに感謝。ありがとう!

4

2 に答える 2

24

これは Spring 4.0+ での新しい動作のようです。コードは Spring の 3.2.x バージョンで問題なく動作します。

その理由は、Spring 4.0+でcustomEditorsin の型が変更されたためと思われます。Spring 3.2.xCustomEditorConfigurerのタイプでしたが、Spring 4.0+ のタイプです。Map<String, ?>Map<Class<?>, Class<? extends PropertyEditor>>

代わりに、次のようにカスタム 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

春にはデフォルトで Date 値の組み込みはありません。ここに行きます: Spring xml構成ファイルでJava Dateオブジェクトを初期化する方法は?

于 2013-09-10T00:04:25.970 に答える