详解Spring自定义属性编辑器

开发 后端
Spring 自定义属性编辑器,Spring DI注入的时候可以把普通属性注入进来,但是像Date类型的就无法被识别。这时可以通过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. }    

 

责任编辑:金贺 来源: JavaEye博客
相关推荐

2024-01-08 08:30:05

光标图形编辑器开发游标

2010-11-16 13:21:08

Oracle命令行

2023-10-20 08:02:25

图形编辑器前端

2009-08-04 13:35:16

ASP.NET自定义样

2011-08-09 17:16:56

CoreAnimati动画

2013-04-01 14:35:10

Android开发Android自定义x

2022-06-20 08:26:39

Spring容器类型转换

2022-09-21 14:42:03

JSProps属性

2023-08-26 19:04:40

配置write转换器

2013-09-10 16:02:59

Elipse编辑器

2009-02-26 08:54:50

FCKeditorHTML编辑器

2011-09-07 09:30:46

Lua编辑器Scite

2009-11-26 17:37:37

Linux编辑器

2009-08-06 17:13:56

ASP.NET自定义控

2011-08-02 11:17:13

iOS开发 View

2013-01-14 11:40:50

IBMdW

2023-10-10 16:04:30

图形编辑器格式转换

2009-12-23 10:27:22

vi编辑器

2021-02-20 11:40:35

SpringBoot占位符开发技术

2010-01-18 15:43:35

VB.NET自定义属性
点赞
收藏

51CTO技术栈公众号