背景
一般情况下,在Java中你可以通过get方法轻松获取beans中的属性值。但是,当你事先不知道beans的类型或者将要访问或修改的属性名时,该怎么办?Java语言中提供了一些像java.beans.Introspector
这样类,实现了在运行时检测Java类并确定属性get和set方法的名称,结合Java中的反射机制就可以调用这些方法了。然而,这些APIs使用起来比较困难,并且将Java类中一些不必要的底层结构暴露给了开发人员。BeanUtils包中的APIs试图简化动态获取和设置bean属性的过程。
BeanUtils包中的PropertyUtils类中的一些静态方法实现了上面的功能,稍后会详细介绍。首先,介绍一些有用的定义:
JavaBean支持的属性类型一般可以划分成三类--标准的JavaBeans规范支持其中的一些,也有一部分只有BeanUtils包支持:
PropertyUtils类中提供了各种API方法用来获取和设置上述三种类型的属性。在下面的程序片段中,假设在bean类中都定义了如下的方法:
public class Employee {
public Address getAddress(String type);
public void setAddress(String type, Address address);
public Employee getSubordinate(int index);
public void setSubordinate(int index, Employee subordinate);
public String getFirstName();
public void setFirstName(String firstName);
public String getLastName();
public void setLastName(String lastName);
}
使用这两个方法,你可以动态地修改employee的name属性:
Employee employee = ...;
String firstName = (String) PropertyUtils.getSimpleProperty(employee, "firstName");
String lastName = (String) PropertyUtils.getSimpleProperty(employee, "lastName");
... manipulate the values ...
PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
PropertyUtils.setSimpleProperty(employee, "lastName", lastName);
Employee employee = ...;
int index = ...;
String name = "subordinate[" + index + "]";
Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, name);
Employee employee = ...;
int index = ...;
Employee subordinate = (Employee) PropertyUtils.getIndexedProperty(employee, "subordinate", index);
Employee employee = ...;
Address address = ...;
PropertyUtils.setMappedProperty(employee, "address(home)", address);
Employee employee = ...;
Address address = ...;
PropertyUtils.setMappedProperty(employee, "address", "home", address);
例如,假设你事实上想要获取的值是employee家庭住址中的city属性。使用标准的Java编程技术直接获取bean的对应属性,你可以这样写:
Employee employee = ...;
String city = (String) PropertyUtils.getProperty(employee, "subordinate[3].address(home).city");
例子:
package com.ztf;
public class Entity {
private Integer id;
private String name;
public void sayHello(){
System.out.println("sayHello()---> 无参");
}
public void sayHello(String s){
System.out.println("sayHello()---> 有1个参数" );
}
public void sayHello(Integer a,Integer b){
System.out.println("sayHello()---> 有2个参数");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
package com.ztf;
import java.util.Map;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class TestPropertyUtils {
public static void main(String[] args) throws Exception{
Entity entity = new Entity();
entity.setId(1) ;
entity.setName("断点");
// 通过PropertyUtils的getProperty方法获取指定属性的值
Integer id = (Integer)PropertyUtils.getProperty(entity, "id");
String name = (String)PropertyUtils.getProperty(entity, "name");
System.out.println("id = " + id + " name = " + name);
// 调用PropertyUtils的setProperty方法设置entity的指定属性
PropertyUtils.setProperty(entity, "name", "每天进步一点");
System.out.println("name = " + entity.getName());
// 通过PropertyUtils的describe方法把entity的所有属性与属性值封装进Map中
Map map = PropertyUtils.describe(entity);
System.out.println("id = " + map.get("id") + " name = " + map.get("name"));
}
其它例子:
import org.apache.commons.beanutils.PropertyUtils;
List
原文链接:http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/package-summary.html#standard