<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

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费国产综合视频在线看 | 激情小说亚洲图片| 久久久久久亚洲精品| 亚洲阿v天堂在线2017免费| 免费真实播放国产乱子伦| 亚洲美国产亚洲AV| 永久免费毛片手机版在线看| 亚洲欧洲免费无码| 免费涩涩在线视频网| 亚洲AV第一成肉网| 亚洲熟妇少妇任你躁在线观看无码 | aa级一级天堂片免费观看| 亚洲国产精品网站久久| 日本精品人妻无码免费大全| 久久久久久亚洲精品影院| 国产免费私拍一区二区三区| 四虎精品成人免费视频| 亚洲伊人久久大香线蕉综合图片| 亚洲精品自在线拍| 野花高清在线电影观看免费视频| 亚洲乱亚洲乱妇24p| avtt亚洲天堂| 大地资源网高清在线观看免费| 亚洲a一级免费视频| 欧美日韩国产免费一区二区三区| 亚洲av日韩综合一区二区三区| 免费在线观看你懂的| 最近免费mv在线观看动漫| 亚洲啪啪免费视频| 精品久久久久久无码免费| 亚洲免费视频在线观看| 中文字幕在线免费观看视频| 亚洲国产精久久久久久久| 无码人妻一区二区三区免费手机| 国产大陆亚洲精品国产| 成年女人色毛片免费看| 特色特黄a毛片高清免费观看| 亚洲国产第一页www| 日本免费人成黄页网观看视频| 免费精品久久天干天干| 中文字幕无码精品亚洲资源网久久|