開発者ドキュメント

Beanプロパティに日付を注入する – CustomDateEditor

Springの例では、Beanプロパティに “Date”を挿入する方法を示します。

package com.mkyong.common;

import java.util.Date;

public class Customer {

    Date date;

    public Date getDate() {
        return date;
    }

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

    @Override
    public String toString() {
        return "Customer[date=" + date + "]";
    }

}

Bean設定ファイル

<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="customer" class="com.mkyong.common.Customer">
        <property name="date" value="2010-01-31"/>
    </bean>

</beans>

それを実行します

package com.mkyong.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[]args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "SpringBeans.xml");

        Customer cust = (Customer) context.getBean("customer");
        System.out.println(cust);

    }
}

次のエラーメッセージが表示されます。

Caused by: org.springframework.beans.TypeMismatchException:
    Failed to convert property value of type[java.lang.String]to
    required type[java.util.Date]for property 'date';

nested exception is java.lang.IllegalArgumentException:
    Cannot convert value of type[java.lang.String]to
    required type[java.util.Date]for property 'date':
    no matching editors or conversion strategy found

解決策

Springでは、次の2つの方法でDateを挿入できます。

1. Factory Bean

“customer” beanのdateFormat beanを宣言します。 “dateFormat” beanをfactory beanとして参照します。ファクトリメソッドは `SimpleDateFormat.parse()`を呼び出してStringをDateオブジェクトに自動的に変換します。

<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="dateFormat" class="java.text.SimpleDateFormat">
        <constructor-arg value="yyyy-MM-dd"/>
    </bean>

    <bean id="customer" class="com.mkyong.common.Customer">
        <property name="date">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="2010-01-31"/>
            </bean>
        </property>
    </bean>

</beans>

2. CustomDateEditor

CustomDateEditorクラスを宣言してStringを

java.util.Date

に変換します。

    <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>

また、別の “CustomEditorConfigurer”を宣言して、Springの型が

java.util.Date

の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>

Bean構成ファイルの完全な例。

<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="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>

    <bean id="customer" class="com.mkyong.common.Customer">
        <property name="date" value="2010-02-31"/>
    </bean>

</beans>

ソースコードをダウンロードする

ダウンロードする – リンク://wp-content/uploads/2010/03/Spring-CustomDateEditor-Examples.zip[Spring-CustomDateEditor-Example.zip](5KB)

モバイルバージョンを終了