[Jakarta Commons笔记] 代码范例 - BeanUtils

假定我们有如下两个标准的JavaBean:



/** Address.java */



package sean.study.commons.beanutils;



public class Address {



private String zipCode;

private String addr;

private String city;

private String country;



public Address() {

}



public Address(String zipCode, String addr, String city, String country) {

this.zipCode = zipCode;

this.addr = addr;

this.city = city;

this.country = country;

}



public String getAddr() {

return addr;

}



public void setAddr(String addr) {

this.addr = addr;

}



public String getCity() {

return city;

}



public void setCity(String city) {

this.city = city;

}



public String getCountry() {

return country;

}



public void setCountry(String country) {

this.country = country;

}



public String getZipCode() {

return zipCode;

}



public void setZipCode(String zipCode) {

this.zipCode = zipCode;

}



}



/** Customer.java */



package sean.study.commons.beanutils;



public class Customer {



private long id;

private String name;

private Address[] addresses;



public Customer() {

}



public Customer(long id, String name, Address[] addresses) {

this.id = id;

this.name = name;

this.addresses = addresses;

}



public Address[] getAddresses() {

return addresses;

}



public void setAddresses(Address[] addresses) {

this.addresses = addresses;

}



public long getId() {

return id;

}



public void setId(long id) {

this.id = id;

}



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}



}





我们来看看通常我们是怎样利用Commons BeanUtils来完成一些基本的JavaBean和DynaBean操作:



package sean.study.commons.beanutils;



import org.apache.commons.beanutils.BasicDynaBean;

import org.apache.commons.beanutils.BasicDynaClass;

import org.apache.commons.beanutils.DynaBean;

import org.apache.commons.beanutils.DynaProperty;

import org.apache.commons.beanutils.PropertyUtils;

import org.apache.commons.lang.StringUtils;



public class BeanUtilsUsage {



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

demoNormalJavaBeans();

demoDynaBeans();

}



public static void demoNormalJavaBeans() throws Exception {



System.out.println(StringUtils.center(" demoNormalJavaBeans ", 40, "="));



// data setup

Address addr1 = new Address("CA1234", "xxx", "Los Angeles", "USA");

Address addr2 = new Address("100000", "xxx", "Beijing", "China");

Address[] addrs = new Address[2];

addrs[0] = addr1;

addrs[1] = addr2;

Customer cust = new Customer(123, "John Smith", addrs);



// accessing the city of first address

String cityPattern = "addresses[0].city";

String name = (String) PropertyUtils.getSimpleProperty(cust, "name");

String city = (String) PropertyUtils.getProperty(cust, cityPattern);

Object[] rawOutput1 = new Object[] { "The city of customer ", name,

"'s first address is ", city, "." };

System.out.println(StringUtils.join(rawOutput1));



// setting the zipcode of customer's second address

String zipPattern = "addresses[1].zipCode";

if (PropertyUtils.isWriteable(cust, zipPattern)) {

System.out.println("Setting zipcode ...");

PropertyUtils.setProperty(cust, zipPattern, "200000");

}

String zip = (String) PropertyUtils.getProperty(cust, zipPattern);

Object[] rawOutput2 = new Object[] { "The zipcode of customer ", name,

"'s second address is now ", zip, "." };

System.out.println(StringUtils.join(rawOutput2));



System.out.println();

}



public static void demoDynaBeans() throws Exception {



System.out.println(StringUtils.center(" demoDynaBeans ", 40, "="));



// creating a DynaBean

DynaProperty[] dynaBeanProperties = new DynaProperty[] {

new DynaProperty("name", String.class),

new DynaProperty("inPrice", Double.class),

new DynaProperty("outPrice", Double.class),

};

BasicDynaClass cargoClass = new BasicDynaClass("Cargo", BasicDynaBean.class, dynaBeanProperties);

DynaBean cargo = cargoClass.newInstance();



// accessing a DynaBean

cargo.set("name", "Instant Noodles");

cargo.set("inPrice", new Double(21.3));

cargo.set("outPrice", new Double(23.8));

System.out.println("name: " + cargo.get("name"));

System.out.println("inPrice: " + cargo.get("inPrice"));

System.out.println("outPrice: " + cargo.get("outPrice"));



System.out.println();

}



}



上述代码运行结果如下:



========= demoNormalJavaBeans ==========

The city of customer John Smith's first address is Los Angeles.

Setting zipcode ...

The zipcode of customer John Smith's second address is now 200000.



============ demoDynaBeans =============

name: Instant Noodles

inPrice: 21.3

outPrice: 23.8



以上代码简单说明了一下BeanUtils常见的基本用法,还有很多高阶或者更具体的应用及原理,这里无法一一讲到,而且有很多笔者也不熟悉、不了解,对BeanUtils的讲解就到此吧。如果你有兴趣,或者还不是很清楚为什么像这样动态的或者说松散的访问JavaBean是有必要的,可以把Struts的源码拿下来研究一下,看看FormBean以及DynaActionForm这些是如何被动态创建的,一定会有收获。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值