Spring如何注入Date类型的变量

58 篇文章 0 订阅

 

地址: http://k2java.blogspot.com/2011/04/spring-how-to-pass-date-into-bean.html

 

Thursday, April 14, 2011

Spring – How to pass a Date into bean property (CustomDateEditor )


Simple method may not work
Generally, Spring developer are not allow to pass a date format parameter into bean property via DI.

For example,

publicclass CustomerService

{

    Date date;

 

    publicDate getDate(){

        return date;

    }

 

    publicvoid setDate(Date date){

        this.date = date;

    }

 

}


Bean configuration file

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

 

   <beanid="customerService"class="com.services.CustomerService">

       <propertyname="date"value="2010-01-31"/>

   </bean>

 

</beans>


Run it

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.mkyong.customer.services.CustomerService;

 

publicclass App

{

    publicstaticvoid main(String[] args )

    {

        ApplicationContext context =

          new ClassPathXmlApplicationContext(newString[]{"Spring-Customer.xml"});

 

        CustomerService cust =(CustomerService)context.getBean("customerService");

        System.out.println(cust.getDate());

    }

}


Error message prompt.

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

Solution

There are two solutions available.

1. Factory bean

Declare a dateFormat bean, and reference it as a factory bean from the date property. The factory method will call the SimpleDateFormat.parse() menthod to convert the String into Date object automatically.

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

 

   <beanid="dateFormat"class="java.text.SimpleDateFormat">

    <constructor-argvalue="yyyy-MM-dd"/>

   </bean>

 

   <beanid="customerService"class="com.mkyong.customer.services.CustomerService">

       <propertyname="date">

           <beanfactory-bean="dateFormat"factory-method="parse">

            <constructor-argvalue="2010-01-31"/>

        </bean>

    </property>

   </bean>

 

</beans>


2. Property editors (CustomEditorConfigurer + CustomDateEditor)

Declare a CustomDateEditor class to convert the String into java.util.Date properties.

<beanid="dateEditor"

       class="org.springframework.beans.propertyeditors.CustomDateEditor">

 

    <constructor-arg>

        <beanclass="java.text.SimpleDateFormat">

            <constructor-argvalue="yyyy-MM-dd"/>

        </bean>

    </constructor-arg>

    <constructor-argvalue="true"/>

 

  </bean>


Register the CustomDateEditor in CustomEditorConfigurer, so that the Spring will convert the properties whose type is java.util.Date.
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

<beanclass="org.springframework.beans.factory.config.CustomEditorConfigurer">

        <propertyname="customEditors">

            <map>

            <entrykey="java.util.Date">

                <reflocal="dateEditor"/>

            </entry>

            </map>

        </property>

    </bean>


Bean configuration file.

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

 

  <beanid="dateEditor"

    class="org.springframework.beans.propertyeditors.CustomDateEditor">

 

    <constructor-arg>

        <beanclass="java.text.SimpleDateFormat">

            <constructor-argvalue="yyyy-MM-dd"/>

        </bean>

    </constructor-arg>

    <constructor-argvalue="true"/>

 

  </bean>

 

  <beanclass="org.springframework.beans.factory.config.CustomEditorConfigurer">

    <propertyname="customEditors">

       <map>

        <entrykey="java.util.Date">

            <reflocal="dateEditor"/>

        </entry>

       </map>

    </property>

   </bean>

 

   <beanid="customerService"class="com.mkyong.customer.services.CustomerService">

       <propertyname="date"value="2010-02-31"/>

   </bean>

 

</beans>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值