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

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

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

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220

    屬性編輯器 公共注入

    Posted on 2009-02-16 19:46 啥都寫點 閱讀(167) 評論(0)  編輯  收藏 所屬分類: J2EE

    1、 spring的普通屬性注入

    參見:spring文檔3.3章節

    當有日期的時候 private Data d;

    轉換不了,報錯,需要自己編寫屬性編輯器

    例:

      

    import java.beans.PropertyEditorSupport;

    public class UtilDatePropertyEditor extends PropertyEditorSupport {

        private String format="yyyy-mm-dd";

       

        //復寫setAsText方法

        publicvoid setAsText(String arg0) throws IllegalArgumentException {

          

           System.out.println("UtilDataPropertyEditor.saveAsText()--text"+arg0);

          

           SimpleDateFormat sdf = new SimpleDateFormat(format);

           try {

               Date d = sdf.parse(arg0);

               this.setValue(d);

           } catch (ParseException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

                }

     }

    }

    //寫完后需要注冊,配置在SPRING配置文件中(注入)

        <bean id="customEditorConfigurer" class="org.springframework.bean.factory.config.CustomEditorConfigurer">

           <property name="customEditors">

              <map>

                 <entry key="java.util.Date">

                   <bean class="com.bjsxt.spring.UtilDatePropertEditor">

    <!-- 

    <property name=”format” value=”yyyy/mm/dd” />

    -->

    </bean>

                 </entry>

              </map>

           </property>

        </bean>

         <!--

         <bean id="utilDatePropertyEditor" class="com.bjsxt.spring.UtilDatePropertEditor"></bean>

    -->

    什么是屬性編輯器,作用:

    *自定義屬性編輯器,spring配置文件中的字符串轉換成相應的對象進行注入

    Spring已經有內置的屬性編輯器,我們可以根據需求自己定義屬性編輯器

    *如何定義屬性編輯器?

    *集成PropertyEditorSupport類,覆寫setAsText()方法,參見:UtilDatePropertyEditor.java

    *將屬性編輯器注冊到spring中,參見:applicationContext-editor.xml

            依賴對象的注入方式,可以采用:

    * ref屬性

    * <ref>標簽

    *內部<bean>來定義

     Spring提供的其他定制編輯器包括:
      ClassEditor—使用包含全稱類名的字符串設置java.lang.
         Class的屬性。
      CustomDateEditor—使用某種 java.text.DateFomat 對
         象將一個字符串設置給 java.util.Date 屬性。                                          
      FileEditor—使用包含文件路徑的字符串設置java.io.File
         的屬性。
      StringArrayPropertyEditor—將一個包含逗號的String
         轉換成String數組屬性。
      StringTrimmerEditor—自動修正字符串屬性,可以選
         擇將空字符轉變成null。 
     
    一個屬性編輯器的簡單示例:
      
      Contact類:

    public class Contact {
       
    private  PhoneNumber   phonenumber;
       
    public void doSomething() {
       System.out.println(phonenumber);
       }


    …..省略getter 、setter方法
    }

    PhoneNumber類:

    public class PhoneNumber {
        
    private String areaCode;
        
    private String prefix;
        
    private String number;
        
        PhoneNumber(String areaCode,
            String prefix,
            String number) 
    {
        
    this.areaCode = areaCode;
        
    this.prefix = prefix;
        
    this.number = number;
        }

        
    public void getName() {
        System.out.println(
    this.areaCode+this.prefix+this.number);
        }

    }

    編輯器類:

    public class PhoneEditor extends PropertyEditorSupport {
       
    private String stripNonNumberic(String original) {
    StringBuffer allNumberic 
    = new StringBuffer();   
       
    for (int i = 0; i < original.length(); i++{
       
    char c = original.charAt(i);
       
    if(Character.isDigit(c)){
       allNumberic.append(c);
          }

     }

       
    return allNumberic.toString();
       }

    public void setAsText(String text) throws IllegalArgumentException {

    String  stripped 
    = stripNonNumberic(text);
    String areaCode 
    = stripped.substring(0,3).replace("4""f");
    String prefix 
    = stripped.substring(3,6).replace("6""g");
    String number 
    = stripped.substring(6,7);
    PhoneNumber phone 
    = new PhoneNumber(areaCode,prefix,number);
    setValue(phone);
          }

    }

    applicationContext.xml中的配置

     <bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
       
    <property name="customEditors">
         
    <map>
           
    <entry key="com.spring.propertyeditor.PhoneNumber">
             
    <bean class="com.spring.propertyeditor.PhoneEditor">
             
    </bean>
           
    </entry>
           
    <entry key="java.util.Date">
              
    <bean class="com.spring.propertyeditor.UtilDatePropertyEditor"></bean>
           
    </entry>
         
    </map>
       
    </property>
    </bean>
     
    <bean id="contact" class="com.spring.propertyeditor.Contact">
       
    <property name="phonenumber">
         
    <value>4564679879412135464546</value>
       
    </property>
       
    <property name="date">
         
    <value>1986-04-11</value>
       
    </property>
     
    </bean>

    測試類代碼:

    public static void main(String[] args) {
    ApplicationContext ac 
    = new ClassPathXmlApplicationContext
                                                                            (
    "applicationContext-propertyeditor.xml");

       Contact con 
    = (Contact)ac.getBean("contact");

        con.getPhonenumber().getName();

    }


    運行結果:f564g79


         
      

    如何將公共的注入定義描述出來?

       *通過<bean>標簽定義公共的屬性,指定abstract=true

       *具有相同屬性的類在<bean>標簽中指定其parent屬性

    參加:applicationContext-other.xml

        <bean id="beanAbstract" abstract="true">

           <property name="id" value=""/>

           <property name="name" value=""/>

        </bean>

        <bean id="bean3" class="" parent="beanAbstract">

           <property name="password">

           <value></value>

           </property>

        </bean>

     <bean id="" class="" parent="beanAbstract"/>



                                                                                                           --    學海無涯
            

    主站蜘蛛池模板: 精品无码专区亚洲| 亚洲成人免费电影| caoporn国产精品免费| 免费a级黄色毛片| 国产成人综合久久精品亚洲| 成人永久免费高清| 国产午夜亚洲精品不卡电影| 国产嫩草影院精品免费网址| 免费观看四虎精品成人| 中文字幕亚洲乱码熟女一区二区 | 女人被男人桶得好爽免费视频| 亚洲va在线va天堂成人| 午夜老司机免费视频| 亚洲AV无码AV男人的天堂不卡| 波多野结衣一区二区免费视频| 成人免费网站久久久| 亚洲成亚洲乱码一二三四区软件| 久久一区二区三区免费播放| 永久免费精品影视网站| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲一级片在线播放| 在线观看成人免费| 一区二区三区在线免费观看视频| 亚洲午夜国产精品无码老牛影视| 久久精品无码精品免费专区| 亚洲 自拍 另类小说综合图区| 国产日韩精品无码区免费专区国产 | 久久精品无码专区免费| 久久亚洲成a人片| 三年片在线观看免费观看高清电影 | 亚洲色大成网站www永久| sihu国产精品永久免费| 亚洲AV无码久久精品狠狠爱浪潮| 1024免费福利永久观看网站| 国产亚洲欧美在线观看| 久久久久久亚洲精品中文字幕| 成人免费在线观看网站| aa级女人大片喷水视频免费| 精品久久久久久亚洲精品| 亚洲人成网站在线观看青青 | free哆啪啪免费永久|