初用apache.commons.beanutils.BeanUtils

 
引言

該class提供了一系列的靜態方法操作業已存在的符合JavaBean規範定義的Java Class.這裏強調的JavaBean規範,簡單來說就是一個Java Class通過一系列getter和setter的方法向外界展示其內在的成員變量(屬性).通過BeanUtils的靜態方法,我們可以:

  • 複製一個JavaBean的實例--BeanUtils.cloneBean();
  • 在一個JavaBean的兩個實例之間複製屬性--BeanUtils.copyProperties(),BeanUtils.copyProperty();
  • 爲一個JavaBean的實例設置成員變量(屬性)值--BeanUtils.populate(),BeanUtils.setProperty();
  • 從一個一個JavaBean的實例中讀取成員變量(屬性)的值--BeanUtils.getArrayProperty(),BeanUtils.getIndexedProperty(),BeanUtils.getMappedProperty(),BeanUtils.getNestedProperty(),BeanUtils.getSimpleProperty(),BeanUtils.getProperty(),BeanUtils.describe();

總的來看BeanUtils類提供了兩大類的功能:讀,寫成員變量.

準備工作

下面逐一分析使用方法.首先我們建立兩個JavaBean,名位SampleObject和SampleObjectA,具體如下:

package beanutil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author samepoint
 *
 * SampleObject contains some types of member varaibles:String,int,Array,Map,Object(self defined),just for test usaged of apache.commons.beanutils.BeanUtils
 */
public class SampleObject {
 String name = null;
 String display = null;
 int num = -1;
 char[] words = {'a','b','c','d'};
 boolean tag = false;
 Map map = new HashMap();
 SampleObjectA sample = null;

 /**
  * default constructor. initialized members of map and sample.
  */
 public SampleObject() {
  this.map.put("home","localhost");
  this.map.put("port","80");
 }

//the following is getters and setters
 /**
  * @return Returns the display.
  */
 public String getDisplay() {
  return display;
 }
 /**
  * @param display The display to set.
  */
 public void setDisplay(String display) {
  this.display = display;
 }
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return Returns the num.
  */
 public int getNum() {
  return num;
 }
 /**
  * @param num The num to set.
  */
 public void setNum(int num) {
  this.num = num;
 }
 /**
  * @return Returns the words.
  */
 public char[] getWords() {
  return words;
 }
 /**
  * @param words The words to set.
  */
 public void setWords(char[] words) {
  this.words = words;
 }
 /**
  * @return Returns the tag.
  */
 public boolean isTag() {
  return tag;
 }
 /**
  * @param tag The tag to set.
  */
 public void setTag(boolean tag) {
  this.tag = tag;
 }
 /**
  * @return Returns the map.
  */
 public Map getMap() {
  return map;
 }
 /**
  * @param map The map to set.
  */
 public void setMap(Map map) {
  this.map = map;
 }
 /**
  * @return Returns the sample.
  */
 public SampleObject getSample() {
  return sample;
 }
 /**
  * @param sample The sample to set.
  */
 public void setSample(SampleObject sample) {
  this.sample = sample;
 }
}

package beanutil;

/**
 * @author samepoint
 *
 * Used to copy properties from SampleOjbect.
 * Used to nested property.
 */
public class SampleObjectA {
 String name = null;
 String display = null;
 Double num = null;

 /**
  * @return Returns the num.
  */
 public Double getNum() {
  return num;
 }
 /**
  * @param num The num to set.
  */
 public void setNum(Double num) {
  this.num = num;
 }
 /**
  * @return Returns the display.
  */
 public String getDisplay() {
  return display;
 }
 /**
  * @param display The display to set.
  */
 public void setDisplay(String display) {
  this.display = display;
 }
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
}

所有測試使用的bean,如果未有說明,均使用SampleObject.

所有測試使用的bean,如果未有說明,均使用SampleObject.
BeanUtils.cloneBean(java.lang.object bean)

爲bean創建一個clone對象,方法返回類型爲Object.注意bean即使沒有實現java.lang.Cloneable接口,此方法依然有效.此方法的實現機制建立在bean提供的一系列的getters和setters的基礎之上.此方法的正常使用代碼非常簡單,故略掉.


下面討論下如果bean沒有提供getters和setters,會出現什麼情況,很明顯如果將其中的一對getter和setter註釋掉,如getDisplay()和setDisplay(),那麼結果是根本不會針對display這個成員變量進行復制;另外,如果將setDisplay()的訪問限定符號設置爲private的話,結果也是一樣的,成員變量-display在clone的過程中不會被複制.注意上面討論的兩種情況,在運行時不會拋出任何的exception.對於不拋出exception的問題,我也感到非常迷惑,因爲此方法的javadoc上明明指出當不能訪問bean上的accessor或不存在accessor時,應該拋出java.lang.IllegalAccessException或java.lang.NotSuchMethodException.爲了再次確認,我將SampleObject中的所有getter和setter都註釋掉了,結果依然一樣,看來要看下源碼了.

BeanUtils.copyProperties(java.lang.Object dest, java.lang.Object orig)

一個bean class有兩個實例:orig和dest,將orig中的成員變量的值複製給dest,即將已經存在的dest變爲orig的副本.與BeanUtils.cloneBean(java.lang.object bean)的區別就在於是不是需要創建新的實例了.同樣正常使用代碼非常簡單,這裏也略掉.
如果bean class中沒有提供或是不完全提供getters和setters,結果如同在BeanUtils.cloneBean(java.lang.object bean)部分中的討論結果一樣.
另外,我曾經這樣想,如果有兩個bean class,他們之間沒有任何關係,只是在成員變量的命名上有重疊(以SampleObject爲例,如果我們有另外的bean class--AnotherSampleObject,也包含了成員變量display,name和num),他們之間是否可以利用BeanUtils.copyProperties(java.lang.Object dest, java.lang.Object orig)進行復制呢?(這個想法來自於<struts in action>中formBean章節中關於formBean與valueObject的討論)答案是可以的,該方法會複製名稱完全一樣的成員變量,即使成員變量的類型不同也會自動進行轉換的(我在AnotherSampleObject中將num的類型定義爲Double,而SampleObject中的num爲int),感覺真的是很神奇.回頭再去看看javadoc,發現這個方法原本就是如此設計的,原文如下:
Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
其中for all cases where the property names are the same正是很好的明證,以後可以放心大膽的使用了.
ps:又對javadoc的重要性進行重新認識,同時認識到自己的英文是那麼的爛.

BeanUtils.copyProperty(java.lang.Object bean,java.lang.String name,java.lang.Object value)

這個方法簡單的說就是將bean中的成員變量name賦值爲value.使用方法如下:
SampleObject sample = new SampleObject();
 BeanUtils.copyProperty(sample,"num",new Integer(10));

如果成員變量爲數組,如何爲數據內的成員賦值呢?apache的java doc上說的很明白,就是要提供一個包含索引參數的setter,所以要將以下代碼加到SampleObject的源代碼中.
/**
  * set word with against
  * @param index
  * @param word
  */
 public void setWords(int index,char word){
  this.words[index] = word;
 }

如果我們要爲SampleObject中的words[2]賦值爲S,那麼代碼如下:
BeanUtils.copyProperty(a,"words[2]","S");

如果成員變量爲Map,如何爲Map內指定key賦值呢?同上面講的數組的方式一樣,就是要提供一個包含key參數的setter,在SampleObject中添加如下代碼:
/**
  * set map with key
  * @param key
  * @param value
  */
 public void setMap(Object key,Object value){
  this.map.put(key,value);
 }

如果我們要將SampleObject.map中home對應值改爲remote,那麼代碼如下:
BeanUtils.copyProperty(a,"map(home)","remote");

最後說下如何爲嵌套屬性的賦值,(所謂嵌套屬性就是beanA中一個成員變量是另外一個beanB,那麼beanB中的屬性就叫做beanA的嵌套屬性了.),用法如下:
BeanUtils.copyProperty(a,"sample.display","second one");

BeanUtils.setProperty(java.lang.Object bean,java.lang.String name,java.lang.Object value)

這個方法讓我鬱悶了一會,因爲它提供的功能與上面說的BeanUtils.copyProperty(java.lang.Object bean,java.lang.String name,java.lang.Object value)完全一致,apache的hero們沒理由爲同一功能提供兩種展示方法啊,後來我看了apache.commons.beanutils.BeanUtilsBean中的javadoc,才明白了一點點.如果我們只是爲bean的屬性賦值的話,使用copyProperty()就可以了;而setProperty()方法是實現BeanUtils.populate()(後面會說到)機制的基礎,也就是說如果我們需要自定義實現populate()方法,那麼我們可以override setProperty()方法.
所以,做爲一般的日常使用,setProperty()方法是不推薦使用的.

BeanUtils.populate(java.lang.Object bean, java.util.Map properties)

使用一個map爲bean賦值,該map中的key的名稱與bean中的成員變量名稱相對應.注意:只有在key和成員變量名稱完全對應的時候,populate機制才發生作用;但是在數量上沒有任何要求,如map中的key如果是成員變量名稱的子集,那麼成員變量中有的而map中不包含的項將會保留默認值;同樣,如果成員變量是map中key的子集,那麼多餘的key不會對populate的結果產生任何影響.恩,結果就是populate只針對map中key名稱集合與bean中成員變量名稱集合的交集產生作用.(很饒口啊)
正常用法很簡單,這裏略掉.
同樣,這個方法也支持對數組中單個元素,map中單個元素和嵌套屬性的賦值,具體做法和copyProperty()方法類似,具體如下:
 values.put("words[1]","U");
  values.put("map(home)","remote");
  values.put("sample.display",new Double(5.0));

注意:apache的javadoc中,明確指明這個方法是爲解析http請求參數特別定義和使用的,在正常的使用中不推薦使用.他們推薦使用BeanUtils.copyProperties()方法.(struts中的FormBean應該是用這個方法裝配的)

BeanUtils.getArrayProperty(java.lang.Object bean,java.lang.String name)

獲取bean中數組成員變量(屬性)的值.
沒什麼好說的,用法很簡單,略.
還是要說一句,如果我們指定的name不是數組類型的成員變量,結果會如何?會不會拋出類型錯誤的exception呢?回答是不會,仍然會返回一個String的數組,數組的第一項就是name對應的值(如果不是String類型的話,JVM會自動的調用toString()方法的).

BeanUtils.getIndexedProperty(java.lang.Object bean,java.lang.String name)
BeanUtils.getIndexedProperty(java.lang.Object bean,java.lang.String name,int index)

這兩個方法都是獲取數組成員變量(屬性)中的單一元素值的方法.比如,我想得到SampleObject中words[1]的值,用法如下:
BeanUtils.getIndexedProperty(sampleOjbectInstance,"words[1]");
 BeanUtils.getIndexedProperty(sampleOjbectInstance,"words",1);

BeanUtils.getMappedProperty(java.lang.Object bean,java.lang.String name)
BeanUtils.getMappedProperty(java.lang.Object bean,java.lang.String name,java.lang.String key)

這兩個方法是獲取map成員變量中單一元素值的方法,用法與getIndexedProperty()方法相似,如我想得到SampleObject中map中home對應的值,用法如下:
BeanUtils.getMappedProperty(sampleOjbectInstance,map(home));
 BeanUtils.getMappedProperty(sampleOjbectInstance,map,"home");

BeanUtils.getNestedProperty(java.lang.Object bean,java.lang.String name)

獲取嵌套屬性值的方法,如我想得到SampleOjbect中成員變量sample中的display的值,用法如下:
BeanUtils.getNestedProperty(sampleOjbectInstance,"sample.display");

BeanUtils.getSimpleProperty(java.lang.Object bean, java.lang.String name)
BeanUtils.getProperty(java.lang.Object bean, java.lang.String name)

獲取屬性值的方法.api已經很清楚了,我唯一的問題是這個simple是什麼意思.javadoc只是說了getProperty()方法中的name參數可以爲普通屬性名稱,數組屬性名稱或嵌套屬性名稱的一種,而getSimpleProperty()方法中的name參數應該爲普通屬性名稱了.我的想法是通過對方法簽名的不同,讓developers可以顯示區別對待普通屬性,數組屬性,map屬性和嵌套屬性.
ps:具體有何區別,看來要仔細看看源代碼了.

BeanUtils.describe(java.lang.Object bean)

將一個bean以map的形式展示.(這個方法和populate()是我夢想中的雙手劍)
但是使用這個方法得到的結果有點令我失望,以SampleObject爲例,代碼片段如下:
 SampleObject a = new SampleObject();
  a.setDisplay("first one");
  a.setName("A");
  a.setNum(5);
  a.setWords("goto".toCharArray());
  SampleObjectA b = new SampleObjectA();
  b.setDisplay("nested property");
  b.setNum(new Double(2.0));
  b.setName("sampleA");
  a.setSample(b);
  try {
   Map descMap = BeanUtils.describe(a);
   System.out.println(descMap);
  }
  ......

運行結果如下:
{num=5, display=first one, class=class beanutil.SampleObject, words=g, tag=false, sample=beanutil.SampleObjectA@be2358, map={port=80, home=localhost}, name=A}

  • 首先可以看出,除了輸出SampleObject中定義的key-value外,還會包含class=class beanutil.SampleObject這一項,我想這是爲了通過獲得的map我們可以知道原來的bean的具體類型;
  • 其次,作爲數組成員變量(屬性)的words,在map中只包含了首個元素,而map類型的成員變量的輸出結果到是非常令人滿意.爲什麼明明長度爲4的words數組現在輸出只有一個字符呢,我又進行了debug,並監控了words變量,發現在返回的descMap中,words對應的值的類型爲String,長度爲1.
    ps:不知道是不是我使用錯誤,真不知道爲什麼會這樣.
  • 最後,嵌套屬性不會逐一進行輸出的,除非你override了toString()方法.
與apache.commons.beanutils.BeanUtilsBean的關係

apache.commons.beanutils.BeanUtils中每個方法是通過apache.commons.beanutils.BeanUtilsBean實現的,apache.commons.beanutils.BeanUtils中靜態方法功能是默認方法,也就是最基本和最普通的,如果需要更復雜的功能實現的話,則需要使用apache.commons.beanutils.BeanUtilsBean中的方法.apache.commons.beanutils.BeanUtilsBean可以在不同的緩衝區內存在不同的實例,從而可以提供不同的服務,主要是converter的不同.通過這個機制可以爲不同的用戶提供本地化的支持(我想這個在internet application上經常要用到吧).我想這也是爲什麼apache.commons.beanutils.BeanUtilsBean不是interface而是class的原因.

總結

BeanUtils是利用java的反射和自醒機制來讀寫javabean的屬性的.
BeanUtils和BeanUtilsBean之間是個什麼設計模式呢?我也搞不清楚.
ps:道行還是太低啊.


相關文章
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章