Spring学习笔记(11)----自定义属性编辑器

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

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

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>  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值