天天看點

《Pro Spring》學習筆記之PropertyEditor

Spring自帶了7種PropertyEditor實作,分别是

ByteArrayPropertyEditor 對應 byte[]屬性

ClassEditor 對應Class屬性

FileEditor對應File屬性

LocaleEditor對應Locale屬性

PropertiesEditor對應Properties屬性

StringArrayPropertyEditor對應String[]屬性

URLEditor對應URL屬性

也就是說,我們以字元串的形式給上述類型的變量注入時候,spring會幫我們自動進行類型轉換,其中要注意的是ClassEditor要求注入的類名字元串不能有任何多餘的空格,否則會有ClassCastException

同時,我們也可以自定義PropertyEditor,本文以一個正則搜尋為例

PatternPropertyEditor.java

《Pro Spring》學習筆記之PropertyEditor

package  ch5.propertyEditor;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

import  java.beans.PropertyEditorSupport;

《Pro Spring》學習筆記之PropertyEditor

import  java.util.regex.Pattern;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public   class  PatternPropertyEditor  extends  PropertyEditorSupport  ... {

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

    public void setAsText(String text) throws IllegalArgumentException ...{

《Pro Spring》學習筆記之PropertyEditor

        Pattern pattern=Pattern.compile(text);

《Pro Spring》學習筆記之PropertyEditor

        setValue(pattern);  //參數是bean的屬性類型對象

《Pro Spring》學習筆記之PropertyEditor

    }

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor

 ExampleBean.java

《Pro Spring》學習筆記之PropertyEditor

package  ch5.propertyEditor;

《Pro Spring》學習筆記之PropertyEditor

import  java.util.regex.Matcher;

《Pro Spring》學習筆記之PropertyEditor

import  java.util.regex.Pattern;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public   class  ExampleBean  ... {

《Pro Spring》學習筆記之PropertyEditor

   private Pattern searchPattern; //注入的正則字元串轉換成Pattern對象,pattern的編譯在CustomerPropertyEditor中完成

《Pro Spring》學習筆記之PropertyEditor

   private String textToSearch;

《Pro Spring》學習筆記之PropertyEditor

   //傳回查找結果數量

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

   public int getMaxCount()...{

《Pro Spring》學習筆記之PropertyEditor

       Matcher matcher=searchPattern.matcher(textToSearch);

《Pro Spring》學習筆記之PropertyEditor

       int count=0;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

       while(matcher.find())...{

《Pro Spring》學習筆記之PropertyEditor

           count++;

《Pro Spring》學習筆記之PropertyEditor

       }

《Pro Spring》學習筆記之PropertyEditor

       return count;

《Pro Spring》學習筆記之PropertyEditor

   }

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public Pattern getSearchPattern() ...{

《Pro Spring》學習筆記之PropertyEditor

    return searchPattern;

《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public void setSearchPattern(Pattern searchPattern) ...{

《Pro Spring》學習筆記之PropertyEditor

    this.searchPattern = searchPattern;

《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public String getTextToSearch() ...{

《Pro Spring》學習筆記之PropertyEditor

    return textToSearch;

《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public void setTextToSearch(String textToSearch) ...{

《Pro Spring》學習筆記之PropertyEditor

    this.textToSearch = textToSearch;

《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor

配置檔案:

《Pro Spring》學習筆記之PropertyEditor

<? xml version="1.0" encoding="UTF-8" ?>

《Pro Spring》學習筆記之PropertyEditor

< beans

《Pro Spring》學習筆記之PropertyEditor

     xmlns ="http://www.springframework.org/schema/beans"

《Pro Spring》學習筆記之PropertyEditor

    xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"

《Pro Spring》學習筆記之PropertyEditor

    xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

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

《Pro Spring》學習筆記之PropertyEditor

   < property  name ="customEditors" >

《Pro Spring》學習筆記之PropertyEditor

     < map >

《Pro Spring》學習筆記之PropertyEditor

       < entry  key ="java.util.regex.Pattern" > <!--  key為注入對應的類型,和ExampleBean中的searchPattern類型保持一緻  -->

《Pro Spring》學習筆記之PropertyEditor

         < bean  class ="ch5.propertyEditor.PatternPropertyEditor" ></ bean >

《Pro Spring》學習筆記之PropertyEditor

       </ entry >

《Pro Spring》學習筆記之PropertyEditor

     </ map >

《Pro Spring》學習筆記之PropertyEditor

   </ property >

《Pro Spring》學習筆記之PropertyEditor

</ bean >

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

< bean  id ="exampleBean"  class ="ch5.propertyEditor.ExampleBean" >

《Pro Spring》學習筆記之PropertyEditor

   < property  name ="searchPattern" >

《Pro Spring》學習筆記之PropertyEditor

     < value > (dog|fish) </ value >

《Pro Spring》學習筆記之PropertyEditor

   </ property >

《Pro Spring》學習筆記之PropertyEditor

   < property  name ="textToSearch" >

《Pro Spring》學習筆記之PropertyEditor

     < value > this is dog and dog and fish and fish and fish </ value >

《Pro Spring》學習筆記之PropertyEditor

   </ property >

《Pro Spring》學習筆記之PropertyEditor

</ bean >

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

</ beans >

測試代碼:

《Pro Spring》學習筆記之PropertyEditor

package  ch5.propertyEditor;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

import  java.io.File;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

import  org.springframework.beans.factory.BeanFactory;

《Pro Spring》學習筆記之PropertyEditor

import  org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

《Pro Spring》學習筆記之PropertyEditor

import  org.springframework.beans.factory.config.CustomEditorConfigurer;

《Pro Spring》學習筆記之PropertyEditor

import  org.springframework.beans.factory.xml.XmlBeanFactory;

《Pro Spring》學習筆記之PropertyEditor

import  org.springframework.core.io.FileSystemResource;

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

public   class  TestSpring ... {

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

  public static void main(String args[])  throws Exception...{

《Pro Spring》學習筆記之PropertyEditor

      //擷取bean factory

《Pro Spring》學習筆記之PropertyEditor

      ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory();  //使用子beanFactory

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

      //配置自動以propertyEditor

《Pro Spring》學習筆記之PropertyEditor

      CustomEditorConfigurer config=(CustomEditorConfigurer)factory.getBean("customEditorConfigure");

《Pro Spring》學習筆記之PropertyEditor

      config.postProcessBeanFactory(factory);

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

      ExampleBean example=(ExampleBean)factory.getBean("exampleBean");

《Pro Spring》學習筆記之PropertyEditor

      System.out.println(example.getMaxCount());

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

  }

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

  public static BeanFactory getBeanFactory()...{

《Pro Spring》學習筆記之PropertyEditor

      //擷取bean factory

《Pro Spring》學習筆記之PropertyEditor

      String realpath="";

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

         //加載配置項

《Pro Spring》學習筆記之PropertyEditor

      realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/propertyEditor"+File.separator+"applicationContext.xml";

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

      ConfigurableListableBeanFactory  factory=new XmlBeanFactory(new FileSystemResource(realpath));

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

      return factory;

《Pro Spring》學習筆記之PropertyEditor

  }

《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor
《Pro Spring》學習筆記之PropertyEditor

}

《Pro Spring》學習筆記之PropertyEditor

運作結果:

5

加載PropertyEditor的方法還有一種,就是調用ConfigurableBeanFactory.registerCustomerEditor方法(參數分别是實用的具體類型合編輯器執行個體,也就是本文的Pattern和PatternPropertyEditor),但并不推薦這樣使用,因為,每當我們定義一個新的CustomerPropertyEditor時候,都需要修改代碼,遠不如聲明式的簡單明了

本文的方法需要注意的是,隻能為Pattern注冊一個PropertyEditor

下一篇: session+cookie

繼續閱讀