关于Spring属性编辑器详解

最近刚在研究Spring 的编辑器,发现很有意思,刚好galaxystar起了一个这样贴,我想对PropertyEditor作一个详细的整理会对大家有益,特定启了这个新帖。
   
    所谓的PropertyEditor,顾名思义,就是属性编辑器。由于Bean属性通过配置文档以字符串了方式为属性赋值,所以必须有一个“东东”负责将这个字符串转换为属性的直接对象,如属性的类型为int,那么编辑器要做的工作就是int i = Integer.parseInt("1");
   Spring 为一般的属性类型提供了默认的编辑器,BeanWrapperImpl是Spring 框架中重要的类,它负责对注入的Bean进行包装化的管理,常见属性类型对应的编辑器即在该类中通过以下代码定义:

Java代码 复制代码
  1. private   void  registerDefaultEditors()   
  2.     {   
  3.          // Simple editors, without parameterization capabilities.   
  4.          // The JDK does not contain a default editor for any of these target types.   
  5.          this .defaultEditors.put( byte []. class new  ByteArrayPropertyEditor());   
  6.          this .defaultEditors.put(Class. class new  ClassEditor());   
  7.          this .defaultEditors.put(File. class new  FileEditor());   
  8.          this .defaultEditors.put(InputStream. class new  InputStreamEditor());   
  9.          this .defaultEditors.put(Locale. class new  LocaleEditor());   
  10.          this .defaultEditors.put(Properties. class new  PropertiesEditor());   
  11.          this .defaultEditors.put(Resource[]. class ,   
  12.                                  new  ResourceArrayPropertyEditor());   
  13.          this .defaultEditors.put(String[]. class new  StringArrayPropertyEditor());   
  14.          this .defaultEditors.put(URL. class new  URLEditor());   
  15.   
  16.          // Default instances of collection editors.   
  17.          // Can be overridden by registering custom instances of those as custom editors.   
  18.          this .defaultEditors.put(Collection. class ,   
  19.                                  new  CustomCollectionEditor(Collection. class ));   
  20.          this .defaultEditors.put(Set. class new  CustomCollectionEditor(Set. class ));   
  21.          this .defaultEditors.put(SortedSet. class ,   
  22.                                  new  CustomCollectionEditor(SortedSet. class ));   
  23.          this .defaultEditors.put(List. class new  CustomCollectionEditor(List. class ));   
  24.   
  25.          // Default instances of character and boolean editors.   
  26.          // Can be overridden by registering custom instances of those as custom editors.   
  27.         PropertyEditor characterEditor =  new  CharacterEditor( false );   
  28.         PropertyEditor booleanEditor =  new  CustomBooleanEditor( false );   
  29.   
  30.          // The JDK does not contain a default editor for char!   
  31.          this .defaultEditors.put( char . class , characterEditor);   
  32.          this .defaultEditors.put(Character. class , characterEditor);   
  33.   
  34.          //  Spring 's CustomBooleanEditor accepts more flag values than the JDK's default editor.   
  35.          this .defaultEditors.put( boolean . class , booleanEditor);   
  36.          this .defaultEditors.put(Boolean. class , booleanEditor);   
  37.   
  38.          // The JDK does not contain default editors for number wrapper types!   
  39.          // Override JDK primitive number editors with our own CustomNumberEditor.   
  40.         PropertyEditor byteEditor =  new  CustomNumberEditor(Byte. class false );   
  41.         PropertyEditor shortEditor =  new  CustomNumberEditor(Short. class false );   
  42.         PropertyEditor integerEditor =  new  CustomNumberEditor(Integer. class false );   
  43.         PropertyEditor longEditor =  new  CustomNumberEditor(Long. class false );   
  44.         PropertyEditor floatEditor =  new  CustomNumberEditor(Float. class false );   
  45.         PropertyEditor doubleEditor =  new  CustomNumberEditor(Double. class false );   
  46.   
  47.          this .defaultEditors.put( byte . class , byteEditor);   
  48.          this .defaultEditors.put(Byte. class , byteEditor);   
  49.   
  50.          this .defaultEditors.put( short . class , shortEditor);   
  51.          this .defaultEditors.put(Short. class , shortEditor);   
  52.   
  53.          this .defaultEditors.put( int . class , integerEditor);   
  54.          this .defaultEditors.put(Integer. class , integerEditor);   
  55.   
  56.          this .defaultEditors.put( long . class , longEditor);   
  57.          this .defaultEditors.put(Long. class , longEditor);   
  58.   
  59.          this .defaultEditors.put( float . class , floatEditor);   
  60.          this .defaultEditors.put(Float. class , floatEditor);   
  61.   
  62.          this .defaultEditors.put( double . class , doubleEditor);   
  63.          this .defaultEditors.put(Double. class , doubleEditor);   
  64.   
  65.          this .defaultEditors.put(BigDecimal. class ,   
  66.                                  new  CustomNumberEditor(BigDecimal. class false ));   
  67.          this .defaultEditors.put(BigInteger. class ,   
  68.                                  new  CustomNumberEditor(BigInteger. class false ));   
  69.     }  


   但是,并非Bean的属性都是这些常见的类型,如果你的Bean需要注入一个自定义类型的属性,而又想享受IoC的好处,那么就只得自己开干,提供一个自定义的PropertyEditor了。
   下面,分几个步骤来说明,定义一个自定义PropertyEditor的过程。
  1)首先,碰到的问题即是,要如何编辑自己的PropertyEditor,其实需要了解一点java.beans包的知识,在该包中,有一个java.beans.PropertyEditor的接口,它定义了一套接口方法(12个),即通过这些方法如何将一个String变成内部的一个对象,这两个方法是比较重要的:
    a)setValue(Object value) 直接设置一个对象,一般不直接用该方法设置属性对象
     b)setAsText(String text) 通过一个字符串来构造对象,一般在此方法中解析字符串,将构造一个
     类对象,调用setValue(Object)来完成属性对象设置操作。
 
  2)实现所有的接口方法是麻烦的,java.beans.PropertyEditorSupport 适时登场,一般情况下,我们通过扩展这个方便类即可。

  3)编写完后,就是在Spring 配置文件中注册该属性类型编辑器的问题,Spring 提供了专门的注册工具类
   org.springframework.beans.factory.config.CustomEditorConfigurer,它负责将属性类型和
   属性编辑器关联起来。到时BeanFactory注入Bean的属性时,即会在注册表中查找属性类型对应的编辑器。

  下面给出一个小例子,例子先作一个简单描述:
  1)Person 需要进行属性注入的Bean,有两个属性 一个是name,一个是address Address是一个类
  2)Address Person的属性类型,本身有3个属性。
  3)AddressPropertyEditor Address类型对应的属性编辑器。
 
  开工:
  1.Person.java

Java代码 复制代码
  1. package  com.stamen.propedit;   
  2.   
  3. import  org.apache.commons.lang.builder.ToStringBuilder;   
  4.   
  5.   
  6. public   class  Person {   
  7.      private  String name;   
  8.   
  9.      private  Address address;   
  10.   
  11.   
  12.      public  Address getAddress() {   
  13.          return  address;   
  14.     }   
  15.   
  16.      public   void  setAddress(Address address) {   
  17.          this .address = address;   
  18.     }   
  19.   
  20.      public  String getName() {   
  21.          return  name;   
  22.     }   
  23.   
  24.      public   void  setName(String name) {   
  25.          this .name = name;   
  26.     }   
  27.      public  String toString() {   
  28.          return  ToStringBuilder.reflectionToString( this );   
  29.     }   
  30.  }   



  2.Address.java

Java代码 复制代码
  1. package  com.stamen.propedit;   
  2. import  org.apache.commons.lang.builder.ToStringBuilder;   
  3. public   class  Address {   
  4.      private  String street;   
  5.   
  6.      private  String doorNum;   
  7.   
  8.      private  String postCode;   
  9.   
  10.      public  String getDoorNum() {   
  11.          return  doorNum;   
  12.     }   
  13.   
  14.      public   void  setDoorNum(String doorNum) {   
  15.          this .doorNum = doorNum;   
  16.     }   
  17.   
  18.      public  String getPostCode() {   
  19.          return  postCode;   
  20.     }   
  21.   
  22.      public   void  setPostCode(String postCode) {   
  23.          this .postCode = postCode;   
  24.     }   
  25.   
  26.      public  String getStreet() {   
  27.          return  street;   
  28.     }   
  29.   
  30.      public   void  setStreet(String street) {   
  31.          this .street = street;   
  32.     }   
  33.        
  34.      public  String toString() {   
  35.          return  ToStringBuilder.reflectionToString( this );   
  36.     }   
  37.   
  38. }   
  39.    


 
AddressPropertyEditor.java

Java代码 复制代码
  1. package  com.stamen.propedit;   
  2.   
  3. import  java.beans.PropertyEditorSupport;   
  4. import  java.util.Date;   
  5.   
  6. import  org.springframework.util.StringUtils;   
  7. public   class  AddressPropertyEditor  extends  PropertyEditorSupport{   
  8.      //支持的格式为 streeValue,doorNumValue,postCode   
  9.      public   void  setAsText(String text)   
  10.     {   
  11.         System.out.println( "使用自己的编辑器。" );   
  12.          if  (text ==  null  || !StringUtils.hasText(text)) {   
  13.              throw   new  IllegalArgumentException( "老大,不能为空啊!" );   
  14.         }   
  15.          else   
  16.         {   
  17.             String[] strArr = StringUtils.tokenizeToStringArray(text, "," );   
  18.             Address add =  new  Address();   
  19.             add.setStreet(strArr[ 0 ]);   
  20.             add.setDoorNum(strArr[ 1 ]);   
  21.             add.setPostCode(strArr[ 2 ]);   
  22.             setValue(add);   
  23.         }   
  24.     }   
  25.        
  26.      public  String getAsText()   
  27.     {   
  28.         Address add = (Address)getValue();   
  29.          return   "" +add;   
  30.     }   
  31. }   
  32.    



打开Spring 配置文件,添上这两个配置项:

Java代码 复制代码
  1.   <bean id= "customEditorConfigurer"    class = "org.springframework.beans.factory.config.CustomEditorConfigurer" >   
  2.   <property name= "customEditors" >   
  3.     <map>   
  4.       <entry key= "com.stamen.propedit.Address" > <!-- 属性类型 -->   
  5.         <bean  class = "com.stamen.propedit.AddressPropertyEditor" /> <!--对应Address的编辑器 -->   
  6.       </entry>   
  7.     </map>   
  8.   </property>   
  9. </bean>   
  10.   
  11.  <bean id= "person"   class = "com.stamen.propedit.Person" >   
  12.     <property name= "name"  value= "Tom" />   
  13.     <property name= "address"  value= "朝阳区,Soho 1601,010101" />   
  14.  </bean>  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值