---
title: "Sping中自定义属性编辑器"
author: "caoyinghui"
date: "2010-03-05T10:55:11Z"
tags: [".net", "Bean", "Blog", "Spring", "XML"]
categories: ["企业架构"]
slug: "sping"
draft: false
lastmod: "2010-03-05T11:18:07Z"
id: 608486
url: "https://caoyinghui.iteye.com/blog/608486"
---

Spring通过PropertyEdit(属性编辑器) 可以将字符串转换为真实类型。通过CustomEditorConfigurer ,ApplicationContext 可以很方便的支持自定义  
PropertyEdit。

MyType.java

package com.cao.spring.applicationContext;

public class MyType {  
 private String text;  
   
 public MyType(String text){  
  this.text = text;  
 }  
   
 public String getText(){  
  return this.text;  
 }  
}  
   
   
DependsOnType.java  
   
package com.cao.spring.applicationContext;

public class DependsOnType {  
 private MyType type;

 public MyType getType() {  
  return type;  
 }

 public void setType(MyType type) {  
  this.type = type;  
 }  
   
}  
   
//自定义的属性编辑器MyTypeEdit.java  
   
package com.cao.spring.applicationContext;

public class MyTypeEdit extends java.beans.PropertyEditorSupport{  
 //提供一种对字符串的转换策略  
 private String format;  
   
   
 public String getFormat() {  
  return format;  
 }

 public void setFormat(String format) {  
  this.format = format;  
 }

 //覆盖父类(PropertyEditorSupport)的setAsText方法。  
  public void setAsText(String text) {  
   if(format!=null && format.equals("upperCase")){  
    System.out.println("修改前的样子:"+text);  
    text=text.toUpperCase();  
   }  
    
  //获得编辑前的类型  
   System.out.println("获得编辑前的类型 "+text.getClass().getSimpleName());  
  //包装成真实类型  
   MyType type = new MyType(text);  
  //注入包装后的类型  
   setValue(type);  
    }  
}

   
配置bean   propertyEdit.xml

   
&lt;beans&gt;  
   &lt;bean id="myBean" class="com.cao.spring.applicationContext.DependsOnType"&gt;  
   &lt;!-- type的真实类型是 MyType 但这里指定的是一个普通的String --&gt;   
       &lt;property name="type"&gt;  
           &lt;value&gt;abc&lt;/value&gt;  
      &lt;/property&gt;  
    &lt;/bean&gt;  
&lt;/beans&gt;

   
将属性编辑器配置进来 plugin.xml  
&lt;bean id="aaa" class="org.springframework.beans.factory.config.CustomEditorConfigurer"&gt;  
      &lt;property name="customEditors"&gt;    
          &lt;map&gt;       &lt;!-- key指定了转换后的类型 --&gt;    
    &lt;entry key="com.cao.spring.applicationContext.MyType"&gt;     
              &lt;!-- 内部bean 配置了自定义的属性编辑器 --&gt;    
             &lt;bean class="com.cao.spring.applicationContext.MyTypeEdit"&gt;       
               &lt;!-- 配置字符串的转换策略 --&gt;      
             &lt;property name="format" value="upperCase"/&gt;    
            &lt;/bean&gt;    
         &lt;/entry&gt;    
          &lt;/map&gt;   
      &lt;/property&gt;  
&lt;/bean&gt;

   
测试类:  
public class MyEditorTest {  
 public static void main(String\[] args) {  
  ApplicationContext ctx = new ClassPathXmlApplicationContext(new String\[]{"application/plugin.xml","application/propertyEdit.xml"});  
  DependsOnType type=  (DependsOnType) ctx.getBean("myBean");  
  System.out.println(type.getType().getClass().getSimpleName());  
  System.out.println(type.getType().getText());  
 }  
}  
   
//输出结果:  
修改前的样子:abc获得编辑前的类型 StringMyTypeABC

 

本文来自CSDN博客，转载请标明出处：[http://blog.csdn.net/caoyinghui1986/archive/2008/05/30/2497473.aspx](http://blog.csdn.net/caoyinghui1986/archive/2008/05/30/2497473.aspx)