来自于:http://hi.baidu.com/universeioi/blog/item/ab2ee5efab85493127979154.html
前面我们所定义的属性都是几本的属性,如果我们定义一个属性是Date类型,例如如下类中:
Java代码 复制代码
1. package com.szy.spring.bean;
2.
3. import java.util.Date;
4.
5. public class Bean {
6. private Date date;
7.
8. public Date getDate()
9. {
10. return date;
11. }
12. public void setDate(Date date)
13. {
14. this.date = date;
15. }
16. }
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;
}
}
按照我们以前学过的知识我们需要在配置文件中给该属性注入值
Xml代码 复制代码
1. <bean id="bean" class="com.szy.spring.bean.Bean">
2. <property name="date" value="2009-11-21"/>
3. </bean>
<bean id="bean" class="com.szy.spring.bean.Bean">
<property name="date" value="2009-11-21"/>
</bean>
下面我们测试是否成功注入值
Java代码 复制代码
1. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2. Bean bean = (Bean)ctx.getBean("bean");
3. System.out.println(bean.getDate());
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean = (Bean)ctx.getBean("bean");
System.out.println(bean.getDate());
运行包如下异常
Exception代码 复制代码
1. 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
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类型的值。
Java代码 复制代码
1. package com.szy.spring.util;
2.
3. import java.beans.PropertyEditorSupport;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. public class DatePropertyEditor extends PropertyEditorSupport
8. {
9.
10. @Override
11. public void setAsText(String text) throws IllegalArgumentException
12. {
13. String format="yyyy-MM-dd";
14. SimpleDateFormat sdf=new SimpleDateFormat(format);
15. try
16. {
17. Date date=sdf.parse(text);
18. this.setValue(date); //把转换后的值传过去
19. } catch (Exception e)
20. {
21. e.printStackTrace();
22. }
23. }
24.
25. }
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代码 复制代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:context="http://www.springframework.org/schema/context"
5. xmlns:tx="http://www.springframework.org/schema/tx"
6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
10. <!-- 把值注入到CustomEditorConfigurer的 Map类型的customEditors属性-->
11. <property name="customEditors">
12. <map>
13. <entry key="java.util.Date">
14. <!-- 内部bean只供自己使用 -->
15. <bean class="com.szy.spring.util.DatePropertyEditor"/>
16. </entry>
17. </map>
18. </property>
19. </bean>
20.
21. </beans>
<?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>
下面我们修改下测试代码已读取所有的配置文件
Java代码 复制代码
1. ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");
2. Bean bean = (Bean)ctx.getBean("bean");
3. System.out.println(bean.getDate());
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支持注入,那么我们为什么不对格式进行注入呢?
修改属性编辑器类:
Java代码 复制代码
1. package com.szy.spring.util;
2.
3. import java.beans.PropertyEditorSupport;
4. import java.text.SimpleDateFormat;
5. import java.util.Date;
6.
7. public class DatePropertyEditor extends PropertyEditorSupport
8. {
9.
10. private String format;
11. @Override
12. public void setAsText(String text) throws IllegalArgumentException
13. {
14.
15. SimpleDateFormat sdf=new SimpleDateFormat(format);
16. try
17. {
18. Date date=sdf.parse(text);
19. this.setValue(date); //把转换后的值传过去
20. } catch (Exception e)
21. {
22. e.printStackTrace();
23. }
24. }
25. public String getFormat()
26. {
27. return format;
28. }
29. public void setFormat(String format)
30. {
31. this.format = format;
32. }
33. }
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添加属性节点
Xml代码 复制代码
1. <bean class="com.szy.spring.util.DatePropertyEditor">
2. <property name="format" value="yyyy/MM/dd"></property>
3. </bean>
<bean class="com.szy.spring.util.DatePropertyEditor">
<property name="format" value="yyyy/MM/dd"></property>
</bean>
下次只要我们修改配置文件即可,灵活性很大。
posted on 2010-06-03 21:00
豪情 阅读(684)
评论(0) 编辑 收藏 所属分类:
Spring