`
sillycat
  • 浏览: 2567003 次
  • 性别: 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

    这篇文章将对比分析Apache BeanUtils、PropertyUtils、Spring BeanUtils以及Cglib BeanCopier这四个常用的Bean复制工具的性能和特点。 首先,Apache BeanUtils是Apache Commons项目的一部分,提供了一系列便捷的...

    JavaBean和beanutils工具类的学习

    `getProperty()`和`setProperty()`方法展示了如何通过反射和PropertyDescriptor类直接访问和修改属性,而BeanUtils和PropertyUtils的使用则更加简洁。 总之,JavaBean和BeanUtils工具类的结合使用极大地提高了代码...

    lomok.docx

    本文将重点讨论 Lombok 的使用场景以及如何替代 BeanUtils 和 PropertyUtils 进行属性复制。 #### 二、BeanUtils.copyProperties 与 PropertyUtils.copyProperties 的用法及区别 在 Java 开发中,经常会遇到需要...

    beanutils的jar包

    BeanUtils的`PropertyUtils`类中的`convertType()`方法能够自动将一个对象转换为指定的目标类型,这大大提高了代码的灵活性和可维护性。 此外,BeanUtils库还支持属性的动态设置和获取。`PropertyUtils.getProperty...

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

    Java反射获取class对象方式解析 Java反射获取class对象方式解析是Java反射机制中的一种重要技术,它可以帮助开发者在运行时获取类的信息和对象实例。下面我们将通过示例代码详细介绍Java反射获取class对象方式解析...

    commons-beanutils-1.7.0-src.zip

    - `org.apache.commons.beanutils.PropertyUtils`: 这个类进一步扩展了BeanUtils的功能,提供了更细致的属性操作,如集合属性的处理。 - `org.apache.commons.beanutils.Converter`: 这是用于类型转换的接口,用户...

    commons-beanutils-1.8.0

    在`commons-beanutils-1.8.0.jar`中,主要类有`BeanUtils`和`PropertyUtils`。`BeanUtils`类提供了简单易用的API,如`copyProperties()`方法,可以方便地将一个对象的所有属性复制到另一个对象上。而`PropertyUtils`...

    commons-beanutils-1.7.0.jar.zip

    首先,Apache Commons BeanUtils库的核心特性在于它的`PropertyUtils`类。这个类提供了大量的静态方法,用于无须实例化对象就能操作JavaBean的属性。例如,`getProperty()`方法可以用来获取指定对象的属性值,而`set...

    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...

    commons-beanutils 源码

    - BeanUtils库的设计遵循了模块化原则,各个功能组件相对独立,如`ConvertUtils`专注于类型转换,`PropertyUtils`则专注于属性操作,这种设计使得代码可读性和维护性更强。 7. **扩展性** - BeanUtils提供了许多...

    BeanUtils-jar包

    《BeanUtils-jar包:JavaBean操作的利器》 在Java开发中,BeanUtils-jar包扮演着重要的角色,它是Apache Commons项目中的一个模块,主要为开发者提供了方便、高效的JavaBean操作工具。这个jar包的核心功能是简化了...

    beanutils-1.8.3开发包

    Apache BeanUtils库是Java开发中的一个关键工具,它在标题"beanutils-1.8.3开发包"中被提及,表明这是一个针对该版本的BeanUtils的完整开发资源集合。这个包提供了对JavaBeans属性操作的强大支持,简化了对象属性的...

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

    例如,`PropertyUtils`类提供了对Bean的动态属性访问,即使在运行时不知道属性名的情况下也能进行操作。这在处理动态配置或者元数据时十分实用。 除了基本的get和set操作,BeanUtils还提供了一些高级特性。比如,`...

    beanutils源码

    2. **核心类分析**:重点关注`org.apache.commons.beanutils`包下的类,如`BeanUtils`、`PropertyUtils`和`ConvertUtils`等,理解它们的功能和实现方式。 3. **反射机制的运用**:深入`java.lang.reflect`包,研究...

    commons-beanutils-1.9.4

    // getters and setters } public static void main(String[] args) { User user = new User(); try { BeanUtils.setProperty(user, "name", "John Doe"); } catch (Exception e) { e.printStackTrace(); ...

    BeanUtils jar包+源码

    3. **类型转换**:BeanUtils内部使用了`PropertyUtils`类来处理属性的类型转换,当源对象和目标对象的属性类型不一致时,它会尝试进行自动类型转换。然而,这种转换并不总是成功,对于一些复杂类型的转换,可能需要...

    commons-beanUtils-src

    - `org.apache.commons.beanutils.PropertyUtils`: 提供了更高级的属性访问和复制功能,包括动态属性处理。 - `org.apache.commons.beanutils.Converter`: 定义了转换器接口,用于自定义数据类型转换规则。 - `org....

    commons-beanutils所有版本(1.0-1.9.2).zip

    例如,`PropertyUtils.getProperty()`和`PropertyUtils.setProperty()`可以用来获取和设置属性值。 3. **类型转换**: BeanUtils库还包含一个强大的类型转换机制,允许在不同数据类型之间自动转换,如`ConvertUtils....

Global site tag (gtag.js) - Google Analytics