Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2498144
  • 博文数量: 319
  • 博客积分: 9650
  • 博客等级: 中将
  • 技术积分: 3881
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 21:05
文章分类

全部博文(319)

文章存档

2017年(5)

2016年(10)

2015年(3)

2014年(3)

2013年(10)

2012年(26)

2011年(67)

2010年(186)

2009年(9)

分类: Java

2010-06-02 20:16:14

前面我们所定义的属性都是几本的属性,如果我们定义一个属性是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. }  

 按照我们以前学过的知识我们需要在配置文件中给该属性注入值

Xml代码
  1. <bean id="bean" class="com.szy.spring.bean.Bean">  
  2.         <property name="date" value="2009-11-21"/>  
  3.     bean>  

 下面我们测试是否成功注入值

Java代码
  1. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");   
  2.         Bean bean = (Bean)ctx.getBean("bean");   
  3.         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  

 通过错误提示信息我们得知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. }  

写完编辑器后我们还需要把编辑器注入到spring中。 为了方便管理我们再新建一个配置文件applicationEditor.xml,用来配置属性编辑器

Xml代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns=""  
  3.     xmlns:xsi=""  
  4.     xmlns:context=""  
  5.     xmlns:tx=""  
  6.     xsi:schemaLocation=" /spring-beans-2.5.xsd   
  7.                  /spring-context-2.5.xsd   
  8.                  /spring-tx-2.5.xsd">  
  9.     <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  10.           
  11.         <property name="customEditors">  
  12.             <map>  
  13.                 <entry key="java.util.Date">  
  14.                       
  15.                     <bean class="com.szy.spring.util.DatePropertyEditor"/>  
  16.                 entry>  
  17.             map>  
  18.         property>  
  19.     bean>  
  20.        
  21. beans>  

下面我们修改下测试代码已读取所有的配置文件

Java代码
  1. ApplicationContext ctx=new ClassPathXmlApplicationContext("application*.xml");   
  2.         Bean bean = (Bean)ctx.getBean("bean");   
  3.         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. }  

同时给该类对应的bean添加属性节点

Xml代码
  1. <bean class="com.szy.spring.util.DatePropertyEditor">  
  2.                         <property name="format" value="yyyy/MM/dd">property>  
  3.                     bean>  

下次只要我们修改配置文件即可,灵活性很大。

  • (2.6 MB)
阅读(2242) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~