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 閱讀(123) 評論(0)  編輯  收藏


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


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

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 梅州市| 五家渠市| 林周县| 侯马市| 运城市| 鄂尔多斯市| 延寿县| 洛南县| 建始县| 河池市| 左云县| 卢湾区| 邹城市| 浏阳市| 自贡市| 崇左市| 河源市| 诸城市| 林甸县| 涞水县| 克山县| 饶河县| 山阴县| 汉源县| 喜德县| 大渡口区| 东源县| 营口市| 厦门市| 绥阳县| 桐城市| 定西市| 濉溪县| 佛坪县| 江源县| 镇平县| 会宁县| 东阳市| 永嘉县| 东方市| 凭祥市|