初用apache.commons.beanutils.BeanUtils

2008-02-23 09:38:15来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

引言

该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都注释掉了,结果依然一样,看来要看下源码了.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:学习Java的笔记(5)

下一篇:Bar.java---BallGame