<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    caoyinghui

    Sping中自定義屬性編輯器

    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

     
    <beans>
       <bean id="myBean" class="com.cao.spring.applicationContext.DependsOnType">
       <!-- type的真實類型是 MyType 但這里指定的是一個普通的String --> 
           <property name="type">
               <value>abc</value>
          </property>
        </bean>
    </beans>

     

     
    將屬性編輯器配置進來 plugin.xml
    <bean id="aaa" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
          <property name="customEditors">  
              <map>       <!-- key指定了轉換后的類型 -->  
        <entry key="com.cao.spring.applicationContext.MyType">   
                  <!-- 內部bean 配置了自定義的屬性編輯器 -->  
                 <bean class="com.cao.spring.applicationContext.MyTypeEdit">     
                   <!-- 配置字符串的轉換策略 -->    
                 <property name="format" value="upperCase"/>  
                </bean>  
             </entry>  
              </map> 
          </property>
    </bean>


     
    測試類:
    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

     

    作者:caoyinghui1986 發表于2008-5-30 20:44:00 原文鏈接
    閱讀:311 評論:0 查看評論

    posted on 2008-05-30 12:44 shine_panda 閱讀(124) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2008年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 欧洲 亚洲 国产图片综合| 国产精品亚洲一区二区三区在线| 2022年亚洲午夜一区二区福利 | 无遮免费网站在线入口| 蜜芽亚洲av无码精品色午夜| 怡红院免费的全部视频| 亚洲精品无码乱码成人 | 国产JIZZ中国JIZZ免费看| 亚洲AⅤ视频一区二区三区| 老司机午夜在线视频免费观 | 亚洲精品乱码久久久久久蜜桃不卡 | 国产成人免费ā片在线观看 | 在线看无码的免费网站| 亚洲精品国产肉丝袜久久| 69天堂人成无码麻豆免费视频| 亚洲人和日本人jizz| 午夜免费福利在线观看| 国产亚洲高清在线精品不卡| 亚洲国产成人精品无码久久久久久综合 | 久久影院亚洲一区| 美女视频黄a视频全免费网站色窝| 亚洲国产一区国产亚洲| 国产男女爽爽爽爽爽免费视频| 亚洲最大av资源站无码av网址| 日本不卡在线观看免费v| kk4kk免费视频毛片| 色拍自拍亚洲综合图区| 好吊妞在线成人免费| 精品一区二区三区免费毛片| 最新国产AV无码专区亚洲| 最近2019免费中文字幕6| 亚洲xxxx视频| 久久亚洲高清综合| 日本视频一区在线观看免费| 亚洲精品无码久久久久牙蜜区| 亚洲第一区精品日韩在线播放| 久久精品免费观看| 亚洲精品无码成人片久久不卡 | 人成免费在线视频| 亚洲精品亚洲人成在线麻豆| 日韩免费高清一级毛片在线|