PropertyEditorSupport

来源:互联网 发布:linux 拷贝进度 编辑:程序博客网 时间:2024/05/16 04:35

  Spring 在装配Bean 时可以使用字符串装配其他数据类型,如URL。也就是说Spring 会自动的将String 类型转换成URL类型进行Bean 的属性装配。这是通过JavaBean API 实现的(java.beans.PropertyEditor 接口)。 那么如何通过Spring 配置将String 类型转换成自定义的类型呢?

 

      这首先需要写一个编辑器类,该类用于实现将String 类型转换成您需要的数据类型。这只需要继承JDK 中的 java.beans.PropertyEditorSupport 类来实现自己的编辑器类。然后我们只需要在Spring 的容器中对这个编辑器进行有效的“注册”便可以实现Spring 在装配Bean 时自动的将Strng 类型转换成我们自定义的类型。 这就是今天我要介绍的CustomEditorConfigurer 类用于实现在Spring 中注册自己定义的编辑器。它是Spring 当中一个非常有用的工厂后处理类(工厂后处理通过Spring 的BeanFactoryPostProcessor 接口实现, 它是在Spring 容器启动并初始化之后进行对Spring 容器的操作类)。在Spring 中已经注册了不少编辑器类,他们都用于String 类型转换为其他的数据类型,如URL,Date等。


      最后就是如何配置CustomEditorConfigurer 类。CustomEditorConfigurer 类中有一个customEditor属性,它是一个Map 类型。通过配置它便实现了自定义的编辑器注册。这个Map 的键值对对应着转换类型和编辑器(转换类型是Key,编辑器是Value)。 相信大家已经明白了如何在Spring 中注册自定义的编辑器了吧。自定义编辑器可以简化Spring 的装配Bean。使其更加的简单。不容易发生配置错误。 PS:如果使用Spring 的ApplicationContext容器,那么只需在Spring 的配置文件中进行简单的装配,而对于Bean 工厂可能需要手动的注册才能使用,具体的实现我也不清楚。



在Spring框架中,提供了几个内置的属性编辑器,如FileEditor,ResourceEditor等。要想使用自定义属性编辑器,需要经过两个步骤。

 

  一。定义一个自定义编辑器,可实现PropertyEditor接口或直接继承PropertyEditorSupport类。

 

Java代码  收藏代码
  1. package com.dream.editor;  
  2.   
  3. import com.dream.model.photo.Photo;  
  4. import com.dream.service.standard.PhotoService;  
  5.   
  6. import java.beans.PropertyEditorSupport;  
  7.   
  8. /** 
  9.  * Created by IntelliJ IDEA. 
  10.  * User: Zhong Gang 
  11.  * Date: 11-9-6 
  12.  * Time: 下午10:10 
  13.  */  
  14. public class PhotoEditor extends PropertyEditorSupport {  
  15.     private PhotoService photoService;  
  16.   
  17.     @Override  
  18.     public String getAsText() {  
  19.         Photo photo = (Photo) getValue();  
  20.         return photo.getGuid();  
  21.     }  
  22.   
  23.     @Override  
  24.     public void setAsText(String text) throws IllegalArgumentException {  
  25.         Photo photo = photoService.loadPhotoByGuid(text);  
  26.         setValue(photo);  
  27.     }  
  28. }  

 

  二。注册自定义编辑器

 

  Spring提供了一个PropertyEditorRegistry接口和PropertyEditorRegistrySupport类来自定义一个注册器。其中PropertyEditorRegistrySupport是Spring提供的一个默认实现,里面注册了一些内置的编辑器。

 

  可以在配置文件中注册自定义编辑器,也可以以编程的方式注册自定义编辑器。

 

 

Java代码  收藏代码
  1. String location = "testApplicationContext.xml";  
  2.         Resource resource = new ClassPathResource(location);  
  3.         XmlBeanFactory beanFactory = new XmlBeanFactory(resource);  
  4.         beanFactory.registerCustomEditor(Photo.class, PhotoEditor.class);  
 

 

Xml代码  收藏代码
  1. <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">  
  2.         <property name="customEditors">  
  3.             <map>  
  4.                 <entry key="com.dream.model.photo.Photo">  
  5.                     <ref bean="photoEditor"/>  
  6.                 </entry>  
  7.             </map>  
  8.         </property>  
  9.     </bean>  
  10.   
  11.     <bean id="photoEditor" class="com.dream.editor.PhotoEditor">  
  12.         <property name="photoService" ref="photoService"/>  
  13.     </bean>