7 Spring注入日期到bean属性

作者: 笑Skr人啊 | 来源:发表于2017-07-10 15:45 被阅读72次

这一个Spring例子向您展示如何为bean属性注入一个“日期”。

实体类
package com.gp6.date;

import java.util.Date;

public class DateBean {
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
    
    public String toString() {
        return "Customer [date=" + 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="DateBean" class="com.gp6.date.DateBean">
        <property name="date" value="2017-12-31" />
    </bean> 

</beans>
测试文件
package com.gp6.date;

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

public class DateTest {
     public static void main( String[] args ) {
            
         ApplicationContext context = new ClassPathXmlApplicationContext(
                    "com/gp6/date/date.xml");

         DateBean dateBean = (DateBean) context.getBean("DateBean");
         System.out.println(dateBean.getDate());
    }
}

执行测试文件,会遇到如下错误信息:

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中,可以通过两种方式注入日期:

1. Factory bean

声明一个dateFormat bean,在“customer” Bean,引用 “dateFormat” bean作为一个工厂bean。该工厂方法将调用SimpleDateFormat.parse()自动转换成字符串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="DateBean" class="com.gp6.date.DateBean">
        <property name="date">
            <bean factory-bean="dateFormat" factory-method="parse">
                <constructor-arg value="2015-12-31" />
            </bean>
        </property>
    </bean>
    

</beans>
2. CustomDateEditor

声明一个 CustomDateEditor 类将字符串转换成 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转换 bean 属性,其类型为java.util.Date。

<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 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="DateBean" class="com.gp6.date.DateBean">
        <property name="date" value="2017-12-31" />
    </bean>

</beans>

相关文章

网友评论

    本文标题:7 Spring注入日期到bean属性

    本文链接:https://www.haomeiwen.com/subject/woayhxtx.html