`
sillycat
  • 浏览: 2491386 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

BeanUtils and PropertyUtils in POJO

    博客分类:
  • JAVA
阅读更多
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.csdn.net/congqian1120/archive/2008/01/15/2045339.aspx

http://www.blogjava.net/hexuzhong/archive/2005/11/26/21498.html
分享到:
评论

相关推荐

    Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier

    NULL 博文链接:https://m635674608.iteye.com/blog/2270455

    Java反射获取class对象方式解析

    主要介绍了Java反射获取class对象方式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    BeanUtils_beanutils_

    BeanUtils 是 Apache commons组件的成员之一,主要用于简化JavaBean封装数据的操作。它可以给JavaBean封装一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中。使用第三方工具,需要导入jar包:

    commons-beanutils-1.9.4-API文档-中文版.zip

    赠送jar包:commons-beanutils-1.9.4.jar; 赠送原API文档:commons-beanutils-1.9.4-javadoc.jar; 赠送源代码:commons-beanutils-1.9.4-sources.jar; 赠送Maven依赖信息文件:commons-beanutils-1.9.4.pom; ...

    commons-beanutils-1.9.3

    BeanUtils工具由Apache软件基金组织编写,提供给我们使用,主要解决的问题是:把对象的属性数据封装到对象中。在整个J2EE的编程过程中,我们经常会从各种配置文件中读取相应的数据,需要明白的一点是从配置文件中...

    commons-beanutils-1.9.4-API文档-中英对照版.zip

    赠送jar包:commons-beanutils-1.9.4.jar; 赠送原API文档:commons-beanutils-1.9.4-javadoc.jar; 赠送源代码:commons-beanutils-1.9.4-sources.jar; 赠送Maven依赖信息文件:commons-beanutils-1.9.4.pom; ...

    commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包

    commons-beanutils-1.8.0.jar beanutils.jar beanutils.jar工具包

    BeanUtils工具

    BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品。beanUtils 可以便于对javaBean的属性、对象进行赋值,beanUtils可以将一个MAP集合的数据拷贝到一个javabean对象中。

    BeanUtils教程

    BeanUtils教程BeanUtils教程BeanUtils教程BeanUtils教程BeanUtils教程BeanUtils教程

    commons-beanutils-1.9.2包含全部jar包源码及API文档

    The Java language provides Reflection and Introspection APIs (see the java.lang.reflect and java.beans packages in the JDK Javadocs). However, these APIs can be quite complex to understand and utilize...

    BeanUtils_1.8.3帮助文档

    BeanUtils_1.8.3最新帮助文档,希望对给位有用。。。

    commons-beanutils-1.8.0 jar包下载

    commons-beanutils-1.8.0 jar包: 1.commons-beanutils-1.8.0.jar 2.commons-beanutils-1.8.0-javadoc.jar 3.commons-beanutils-1.8.0-sources.jar 4.commons-beanutils-bean-collections-1.8.0.jar 5.commons-...

    BeanUtils所需的包

    BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包BeanUtils的包

    beanutils 1.8 jar包+源码_阿锦—小白

    完美beanUtils 1.8 jar包与源码

    org.apache.commons.beanutils.BeanUtils实例

    此为BeanUtils的实例。其中apache的包有一个小的BUG已在其中说明。

    Commons-beanutils jar包

    Commons-beanutils jar包,比较新希望对大家能有帮助

    commons-beanutils-1.9.1解析javaBean方式

    Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的。无论谁的项目,始终一天都会用得上它。我算是后知后觉了,第一回看到它的时候居然错过。 1.属性的动态getter,setter 2....

    BeanUtils用法

    BeanUtils用法

    beanutils-1.9.3-bin

    BeanUtils是由Apache公司开发的针对操作JavaBean的工具包。 对于JavaBean,简单的来说,就是要有一个空参的构造器和对属性的getXXX方法和setXXX方法。 在由JDK提供的默认的API中,有java.beans包,里面有诸如...

Global site tag (gtag.js) - Google Analytics