BeanUtils and PropertyUtils in POJO

本文演示了如何使用Apache Commons BeanUtils库来方便地设置Java Bean中的属性值,包括嵌套属性和集合类型的处理。通过一个具体示例,展示了BeanUtils在实际应用中的灵活性与便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BeanUtils and PropertyUtils in POJO

1. my test POJO
Person.java:
package com.sillycat.easymarket.model;
import java.util.Map;
public class Person {
private Integer id;
private String personName;
private String personPassword;
private String gender;
private Integer age;
private Address address;
private Map<String,Object> others;

...snip getter and setter ...

public String toString() {
return "Person [id=" + id + ", personName=" + personName
+ ", personPassword=" + personPassword + ", gender=" + gender
+ ", age=" + age + ", address=" + address + ", others="
+ others + "]";
}
}

Address.java:
package com.sillycat.easymarket.model;
public class Address {
private Integer id;
private String email;
private String city;
private String country;

...snip getter and setter ...

public String toString() {
return "Address [id=" + id + ", email=" + email + ", city=" + city
+ ", country=" + country + "]";
}
}

2. My test Class
BeanFilter.java:
package com.sillycat.easymarket.filter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
public class BeanFilter {
public static void main(String[] args) {
Person p = new Person();
Address a = new Address();
a.setCity("Chengdu");
a.setCountry("China");
a.setEmail("hua.luo@chengdu.digby.com");
a.setId(Integer.valueOf(1));
p.setAddress(a);
p.setAge(Integer.valueOf(29));
p.setGender("man");
p.setPersonName("sillycat");
p.setPersonPassword("test");
p.setId(Integer.valueOf(1));
Map<String, Object> others = new HashMap<String, Object>();
others.put("other1", "good");
others.put("other2", "other requirement");
p.setOthers(others);
System.out.println(p);
System.out.println(p.getOthers());
try {
PropertyUtils.setProperty(p, "personName", null);
PropertyUtils.setProperty(p, "address.city", null);
PropertyUtils.setProperty(p, "others.other2", null);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
System.out.println(p);
System.out.println(p.getOthers());
}
}

3. The console output
Person [id=1, personName=sillycat, personPassword=test, gender=man, age=29, address=Address [id=1, email=hua.luo@chengdu.digby.com, city=Chengdu, country=China], others={other1=good, other2=other requirement}]
{other1=good, other2=other requirement}
Person [id=1, personName=null, personPassword=test, gender=man, age=29, address=Address [id=1, email=hua.luo@chengdu.digby.com, city=null, country=China], others={other1=good, other2=null}]
{other1=good, other2=null}


references:
http://www.4ucode.com/Study/Topic/434994

http://www.cnblogs.com/H_Razor/archive/2011/03/01/1967620.html

http://blog.youkuaiyun.com/congqian1120/archive/2008/01/15/2045339.aspx

http://www.blogjava.net/hexuzhong/archive/2005/11/26/21498.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值