Spring 自定义属性编辑器

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. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  5.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">  
  6.   
  7.     <!-- 普通属性注入 -->  
  8.     <bean id="userBean" class="com.cos.entity.UserBean">  
  9.         <!-- 时间属性,需要属性编辑器 -->  
  10.         <property name="birthday" value="2011-03-16"/>  
  11.     </bean>  
  12.   
  13.     <!-- 特殊属性的注入.把特殊属性注入到CustomEditorConfigurer Bean 里 -->  
  14.     <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  15.         <property name="customEditors">  
  16.             <map>  
  17.                 <entry key="java.util.Date">  
  18.                     <bean class="com.cos.entity.DatePropertyEditor">  
  19.                         <property name="format" value="yyyy-MM-dd"/>  
  20.                     </bean>  
  21.                 </entry>  
  22.             </map>  
  23.         </property>  
  24.     </bean>  
  25. </beans>  

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. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值