详解Spring自定义属性编辑器
创始人
2024-07-23 09:01:45
0

Spring 自定义属性编辑器

Spring DI注入的时候可以把普通属性注入进来,但是像Date类型的就无法被识别。这时可以通过Spring的属性编辑器把配置文件中的字符串转化成相应的对象进行注入。

Spring有自带的属性编辑器,我们也可以写自定义的属性编辑器

自定义属性编辑器:

继承java.beans.PropertyEditorSupport类,重写其中的setAsText(String text)方法。

再把自定义的属性编辑器注入到Spring中。

例子:

JavaBean类

Java代码

 

 

  1. package com.cos.entity;     
  2.     
  3. import java.util.Date;     
  4. import java.util.List;     
  5. import java.util.Map;     
  6. import java.util.Set;     
  7.     
  8. public class UserBean {     
  9.     
  10.     private Date birthday;     
  11.     
  12.     public Date getBirthday() {     
  13.         return birthday;     
  14.     }     
  15.     
  16.     public void setBirthday(Date birthday) {     
  17.         this.birthday = birthday;     
  18.     }     
  19. }    

 

自定义属性编辑器

Java代码

 

 

  1. package com.cos.entity;     
  2.     
  3. import java.beans.PropertyEditorSupport;     
  4. import java.text.ParseException;     
  5. import java.text.SimpleDateFormat;     
  6.     
  7. //自己写一个自定义属性编辑器来继承属性编辑器PropertyEditorSupport     
  8. public class DatePropertyEditor extends PropertyEditorSupport {     
  9.     
  10.     //时间的格式     
  11.     String format;     
  12.     
  13.     public String getFormat() {     
  14.         return format;     
  15.     }     
  16.     
  17.     public void setFormat(String format) {     
  18.         this.format = format;     
  19.     }     
  20.     
  21.     //需要重写属性编辑器的setAsText()方法     
  22.     @Override    
  23.     public void setAsText(String text) {     
  24.         try {     
  25.             SimpleDateFormat f = new SimpleDateFormat(format);     
  26.             //把转换后的值传进去     
  27.             this.setValue(f.parse(text));     
  28.         } catch (ParseException ex) {     
  29.             ex.printStackTrace();     
  30.         }     
  31.     }     
  32. }    

 

spring配置文件 applicationContext.xml :

Xml代码

 

  1.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"    
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  3.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd     
  4.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">    
  5.     
  6.         
  7.         
  8.             
  9.             
  10.         
  11.     
  12.         
  13.         
  14.             
  15.                 
  16.                     
  17.                         
  18.                             
  19.                         
  20.                     
  21.                 
  22.             
  23.         
  24.     

 

 

 

 

 

 

 

 

 

org.springframework.beans.factory.config.CustomEditorConfigurer类可以读取PropertyEditorSupport类及子类,将字符串转化为指定的类型。

PropertyEditorSupport类把要转化的Date类型注入到customEditors Map中。

测试类:

Java代码

 

 

  1. package com.cos.entity;     
  2.     
  3. import org.springframework.beans.factory.BeanFactory;     
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;     
  5.     
  6. public class Main {     
  7.     
  8.     public static void main(String[] args) {     
  9.         //通过spring配置文件返回Bean的工厂对象     
  10.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");     
  11.         //Bean工厂通过Bean的id得到JavaBean     
  12.         UserBean ub = (UserBean) factory.getBean("userBean");     
  13.         System.out.println(""+ub.getBirthday());     
  14.     }     
  15. }    

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...