spring bean 日期格式注入的几种模式

来源:互联网 发布:网站seo工作内容 编辑:程序博客网 时间:2024/06/05 03:20

当我们进行bean注入到spring当中的时候,这个bean当中可能会存在着一些特殊类型的数据元素,如Date类型,当我们不进行一些特殊处理的时候,想直接给其赋值就会报告错误。

我们定义了如下类

Java代码  收藏代码
  1. package com.japie.customproperty;  
  2.   
  3. import java.util.Date;  
  4. /** 
  5.  * 自定义属性配置 
  6.  * @author Japie 
  7.  * 下午06:15:04 
  8.  */  
  9. public class BoyMan {  
  10.     private Date birthDay;  
  11.     private String username;  
  12.   
  13.     public BoyMan() {  
  14.     }  
  15.   
  16.     public Date getBirthDay() {  
  17.         return birthDay;  
  18.     }  
  19.   
  20.     public void setBirthDay(Date birthDay) {  
  21.         this.birthDay = birthDay;  
  22.     }  
  23.   
  24.     public String getUsername() {  
  25.         return username;  
  26.     }  
  27.   
  28.     public void setUsername(String username) {  
  29.         this.username = username;  
  30.     }  
  31.     @Override  
  32.     public String toString()  
  33.     {  
  34.         return "BoyMan:["+username+this.birthDay+"]";  
  35.     }  
  36.     public static void main(String[] args) {  
  37.     }  
  38.   
  39. }  

 配置文件为

Java代码  收藏代码
  1. <bean id="boyMan" class="com.japie.customproperty.BoyMan">  
  2.         <property name="birthDay" value="2011-06-17">  
  3.         <property name="username" value="japie"/>  
  4. </bean>  

 当进行测试的时候报告如下的错误。

Java代码  收藏代码
  1. Caused by: org.springframework.beans.TypeMismatchException:   
  2.     Failed to convert property value of type [java.lang.String] to   
  3.     required type [java.util.Date] for property 'birthDay';  

 2 那么如何解决上面的问题呢,有如下几种方法供参考

Declare a dateFormat bean, in “customer” bean, reference “dateFormat” bean as a factory bean. The factory method will call SimpleDateFormat.parse() to convert String into Date object automatically.

 

    1) 定义一个dateFormat bean,而后通而后通过工程模式在boyman这个类当中用构造器的模式进行引用,其实就是调用SimpleDateFormat.parse() 的方法,配置文件如下

Java代码  收藏代码
  1. <bean id="boyMan" class="com.japie.customproperty.BoyMan">  
  2.         <property name="birthDay">  
  3.          <bean factory-bean="dateFormat" factory-method="parse">  
  4.             <constructor-arg value="2011-06-17" />  
  5.          </bean>  
  6.         </property>  
  7.         <property name="username" value="japie"/>  
  8. </bean>  

 如此就可以进行正常的赋值了。

2)自定义日期熟悉编辑器

 

Java代码  收藏代码
  1. <bean id="dateEditor"  
  2.         class="org.springframework.beans.propertyeditors.CustomDateEditor">  
  3.    
  4.         <constructor-arg>  
  5.             <bean class="java.text.SimpleDateFormat">  
  6.                 <constructor-arg value="yyyy-MM-dd" />  
  7.             </bean>  
  8.         </constructor-arg>  
  9.         <constructor-arg value="true" />  
  10. </bean>  

3』通过重写spring的熟悉方法来实现代码如下

 

Java代码  收藏代码
  1. package com.japie.injection;  
  2.   
  3. import java.beans.PropertyEditorSupport;  
  4. import java.text.ParseException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. /** 
  9.  * java.util.Date属性编辑器 
  10.  * @author japie 
  11.  * 
  12.  */  
  13. public class UtilDatePropertyEditor extends PropertyEditorSupport {  
  14.   
  15.     private String pattern;  
  16.       
  17.     @Override  
  18.     public void setAsText(String text) throws IllegalArgumentException {  
  19.         System.out.println("---UtilDatePropertyEditor.setAsText()--->" + text);  
  20.         try {  
  21.             Date date = new SimpleDateFormat(pattern).parse(text);  
  22.             this.setValue(date);  
  23.         } catch (ParseException e) {  
  24.             e.printStackTrace();  
  25.             throw new IllegalArgumentException(text);  
  26.         }  
  27.     }  
  28.   
  29.     public void setPattern(String pattern) {  
  30.         this.pattern = pattern;  
  31.     }  
  32.   
  33.       
  34. }  
 

以及在我们具体的bean当中引用其的配置

 

Java代码  收藏代码
  1. <bean id="boyMan" class="com.japie.customproperty.BoyMan">  
  2.         <property name="birthDay" value="2011-06-17" />  
  3.         <property name="username" value="japie"/>  
  4. </bean>  
转载自:http://cjjwzs.iteye.com/blog/1097099

0 0
原创粉丝点击