caoyinghui

          Sping中自定義屬性編輯器

          Spring通過PropertyEdit(屬性編輯器) 可以將字符串轉(zhuǎn)換為真實(shí)類型。通過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{
           //提供一種對字符串的轉(zhuǎn)換策略
           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());
            //包裝成真實(shí)類型
             MyType type = new MyType(text);
            //注入包裝后的類型
             setValue(type); 
              }
          }

           

           
          配置bean   propertyEdit.xml

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

           

           
          將屬性編輯器配置進(jìn)來 plugin.xml
          <bean id="aaa" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
                <property name="customEditors">  
                    <map>       <!-- key指定了轉(zhuǎn)換后的類型 -->  
              <entry key="com.cao.spring.applicationContext.MyType">   
                        <!-- 內(nèi)部bean 配置了自定義的屬性編輯器 -->  
                       <bean class="com.cao.spring.applicationContext.MyTypeEdit">     
                         <!-- 配置字符串的轉(zhuǎn)換策略 -->    
                       <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());
           }
          }
           
          //輸出結(jié)果:
          修改前的樣子:abc獲得編輯前的類型 StringMyTypeABC

           

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

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


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


          網(wǎng)站導(dǎo)航:
           
          <2008年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 河曲县| 体育| 三明市| 江陵县| 杂多县| 格尔木市| 田阳县| 晋城| 大埔县| 贵阳市| 卓资县| 长汀县| 新乡市| 麻城市| 昌乐县| 太康县| 建瓯市| 丰镇市| 彰化县| 抚宁县| 尼勒克县| 清镇市| 淮阳县| 图片| 桂阳县| 赣州市| 伽师县| 茶陵县| 娱乐| 隆安县| 西吉县| 惠安县| 合阳县| 阳泉市| 寻乌县| 临颍县| 安化县| 吴旗县| 华坪县| 新宾| 舟曲县|