前面我们所定义的属性都是几本的属性,如果我们定义一个属性是Date类型,例如如下类中:
package com.szy.spring.bean;
import java.util.Date;
public class Bean {
private Date date;
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
按照我们以前学过的知识我们需要在配置文件中给该属性注入值
<bean id="bean" class="com.szy.spring.bean.Bean"> <property name="date" value="2009-11-21"/> </bean>
下面我们测试是否成功注入值
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
运行包如下异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bean' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is 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不能将string转换成date类型,没有匹配的编辑器或者转换机制。
如果想实现string转换成Date,那么我们自己需要写一个属性编辑器
我们新建一个类DatePropertyEditor,这个类要继承PropertyEditorSupport类。
我们需要复写这个类中的setAsText方法,其中text参数就是配置文件中的值。我们的任务就是把text转换成date类型的值。
package com.szy.spring.util;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
@Override
public void setAsText(String text) throws IllegalArgumentException
{
String format="yyyy-MM-dd";
SimpleDateFormat sdf=new SimpleDateFormat(format);
try
{
Date date=sdf.parse(text);
this.setValue(date); //把转换后的值传过去
} catch (Exception e)
{
e.printStackTrace();
}
}
}
写完编辑器后我们还需要把编辑器注入到spring中。 为了方便管理我们再新建一个配置文件applicationEditor.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <!-- 把值注入到CustomEditorConfigurer的 Map类型的customEditors属性--> <property name="customEditors"> <map> <entry key="java.util.Date"> <!-- 内部bean只供自己使用 --> <bean class="com.szy.spring.util.DatePropertyEditor"/> </entry> </map> </property> </bean> </beans>
下面我们修改下测试代码已读取所有的配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
最后测试,成功输出时间。
刚才我们在配置文件中时间的格式是2009-11-21,如果我们修改成2009/11/21呢?
运行报错:Unparseable date: "2009/11/21"
这时我们需要修改属性编辑器类文件的格式了,很麻烦。既然spring支持注入,那么我们为什么不对格式进行注入呢?
修改属性编辑器类:
package com.szy.spring.util;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePropertyEditor extends PropertyEditorSupport
{
private String format;
@Override
public void setAsText(String text) throws IllegalArgumentException
{
SimpleDateFormat sdf=new SimpleDateFormat(format);
try
{
Date date=sdf.parse(text);
this.setValue(date); //把转换后的值传过去
} catch (Exception e)
{
e.printStackTrace();
}
}
public String getFormat()
{
return format;
}
public void setFormat(String format)
{
this.format = format;
}
}
同时给该类对应的bean添加属性节点
<bean class="com.szy.spring.util.DatePropertyEditor"> <property name="format" value="yyyy/MM/dd"></property> </bean>
下次只要我们修改配置文件即可,灵活性很大。