|
引言 该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;
本篇文章共5页,此页为首页 下一页
|