初用org.apache.commons.beanutils.DynaBean

2008-02-23 09:35:28来源:互联网 阅读 ()

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

org.apache.commons.beanutils.DynaBean

DynaBean的具体实现类有四个,分别是BasicDynaBean, LazyDynaBean, ResultSetIterator, WrapDynaBean。

org.apache.commons.beanutils.BasicDynaBean

最基本的实现,如果想要创建一个BasicDynaBean的实例话,要经历以下步骤:

  1. 创建一个org.apache.commons.beanutils.BasicDynaClass的实例,并向其中填充dynaBean的元数据(metadata)
  2. 利用装配完整的org.apache.commons.beanutils.BasicDynaClass的实例创建一个BasicDynaBean的实例。

我将会创建一个名为dynaClass的BasicDynaClass的实例,它包含了四个属性,其中一个是数组,而另外的其中一个是map;然后我会利用dynaClass创建一个名为dynaBean的实例,并对dynaBean进行赋值操作。下面是我的代码:(输出部分略掉)

public class DynaBeanTry {
private DynaClass dynaClass;
private DynaBean dynaBean;
/**
* create basicDynaClass
*/
public void createBasicDynaClass() {
dynaClass = null;
//create basic field for dynaClass
DynaProperty[] dynaProps = new DynaProperty[4];
dynaProps[0] = new DynaProperty("any");
dynaProps[1] = new DynaProperty("nameStr", String.class);
//create array field for dynaClass
String[] nameArray = new String[4];
dynaProps[2] = new DynaProperty("nameArray", nameArray.getClass(),
String.class);
//create map filed for dynaClass
Map nameAliasMap = new HashMap();
dynaProps[3] = new DynaProperty("nameAliasMap",
nameAliasMap.getClass(), String.class);
dynaClass = new BasicDynaClass("first", BasicDynaBean.class, dynaProps);
}

public void createBasicDynaBean() {
dynaBean = null;
this.createBasicDynaClass();
try {
dynaBean = dynaClass.newInstance();
dynaBean.set("any", "bull shit");
dynaBean.set("nameStr", "bull");
dynaBean.set("nameArray", new String[4]);
dynaBean.set("nameArray", 0, "b");
dynaBean.set("nameArray", 1, "u");
dynaBean.set("nameArray", 2, "l");
dynaBean.set("nameArray", 3, "l");
dynaBean.set("nameAliasMap", new HashMap());
dynaBean.set("nameAliasMap", "1st", "b");
dynaBean.set("nameAliasMap", "2nd", "u");
dynaBean.set("nameAliasMap", "3rd", "l");
dynaBean.set("nameAliasMap", "4th", "l");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.descDynaBean(dynaBean);
}
}

在我写上面代码的时候,发现了两个值得注意的地方:

public void createBasicDynaBean() {
dynaBean = null;
this.createBasicDynaClass();
try {
dynaBean = dynaClass.newInstance();
dynaBean.set("any", "bull shit");
dynaBean.set("nameStr", "bull");
dynaBean.set("nameArray", new String[4]);
dynaBean.set("nameArray", 0, "b");
dynaBean.set("nameArray", 1, "u");
dynaBean.set("nameArray", 2, "l");
dynaBean.set("nameArray", 3, "l");
dynaBean.set("nameAliasMap", new HashMap());
dynaBean.set("nameAliasMap", "1st", "b");
dynaBean.set("nameAliasMap", "2nd", "u");
dynaBean.set("nameAliasMap", "3rd", "l");
dynaBean.set("nameAliasMap", "4th", "l");
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.descDynaBean(dynaBean);
}
}

在我写上面代码的时候,发现了两个值得注意的地方:

  1. 在为dynaClass填充metadata的时候,如果要填充的属性是数组、集合或map,我们只需要标识它的类型,无须将其内部元素进行设置。如dynaProps[3] = new DynaProperty("nameAliasMap",nameAliasMap.getClass(), String.class),我并没有对nameAliasMap内部进行key-value的填充。
  2. 在对dynaBean赋值时,如果操作的属性的类型是数组、集合或map,注意:要首先将一个数组、集合或map对象赋值到对应属性上,然后再进行对数组、集合或map内部成员的操作,否则在运行期间我们会得到某某对象不存在之类的错误。

    标签:

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

上一篇:java.util包(二)

下一篇:东软SMIAS Java API移动短信接入JUnit TestCase