PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
2012-10-04 17:37
429 查看
背景
一般情况下,在Java中你可以通过get方法轻松获取beans中的属性值。但是,当你事先不知道beans的类型或者将要访问或修改的属性名时,该怎么办?Java语言中提供了一些像
BeanUtils包中的PropertyUtils类中的一些静态方法实现了上面的功能,稍后会详细介绍。首先,介绍一些有用的定义:
JavaBean支持的属性类型一般可以划分成三类--标准的JavaBeans规范支持其中的一些,也有一部分只有BeanUtils包支持:
Simple(单值) -- 单值或量,有个一可以访问或修改的属性。值的类型可能是Java语言的原生类型(如:int型),简单的类(如:java.lang.String),或者一个复杂类的对象,这个类可能来自Java语言或者来自应用程序再或者来自应用程序中的一个类库。
Indexed(索引) -- 索引的属性,属性中存放有序对象(都是同类型的)的集合,每个对象都可以通过一个非负的整数值(或下标)来获取。另外,所有值的集合可以使用一个数组来设置或者获取。作为一个JavaBeans规范的扩展,BeanUtils包认为所有底层数据类型为java.util.List(或List的一个实现)的属性都可以被索引。
Mapped(映射) -- 作为一个标准JavaBeans APIs的扩展, BeanUtils包认为所有底层数据类型为java.util.Map的属性都可以被"映射"。你可以通过String类型的key值来设置或者获取对应的值。
PropertyUtils类中提供了各种API方法用来获取和设置上述三种类型的属性。在下面的程序片段中,假设在bean类中都定义了如下的方法:
访问基本属性
获取和设置simple属性很简单。在Javadocs中查看下面两个方法:
PropertyUtils.getSimpleProperty(Object bean, String name)
PropertyUtils.setSimpleProperty(Object bean, String name, Object value)
使用这两个方法,你可以动态地修改employee的name属性:
对于indexed(索引)属性,你有两种选择 - 你既可以在属性名后面添加方括号在里面放上一个下标,也可以在调用方法时将其作为一个独立参数:
PropertyUtils.getIndexedProperty(Object bean, String name)
PropertyUtils.getIndexedProperty(Object bean, String name, int index)
PropertyUtils.setIndexedProperty(Object bean, String name, Object value)
PropertyUtils.setIndexedProperty(Object bean, String name, int index, Object value)
属性名的下标只能是整数常量。如果你想获取的项的索引是计算出来的,你可以将属性名和索引作为字符串组合起来。例如,你可以向下面这样做:
类似的,获取和设置mapped(映射)属性的方法同样有两对。与indexed(索引)不同的是额外的属性是用括号括起来的(“(”和“)”)而不是方括号,并且获取和设置值时如同从底层的map中获取和设置值一样。
PropertyUtils.getMappedProperty(Object bean, String name)
PropertyUtils.getMappedProperty(Object bean, String name, String key)
PropertyUtils.setMappedProperty(Object bean, String name, Object value)
PropertyUtils.setMappedProperty(Object bean, String name, String key, Object value)
例如,你可以使用下面两种方法设置employee的家庭住址:
访问嵌套属性
在上面的例子中,我们假设你将bean作为第一个参数传入PropertyUtils方法,并希望获取指定属性的值。然而,如果属性的值是一个Java对象,并且你希望进一步获取这个Java对象的某个属性的值?
例如,假设你事实上想要获取的值是employee家庭住址中的city属性。使用标准的Java编程技术直接获取bean的对应属性,你可以这样写:
[java]
view plaincopy
String city = employee.getAddress("home").getCity();
使用PropertyUtils类中的等效机制被称为嵌套属性访问。使用这种方法,你将访问路径上的属性的名称用“.”拼接起来 --与你在JavaScript执行嵌套属性访问的方式非常相似。
PropertyUtils.getNestedProperty(Object bean, String name)
PropertyUtils.setNestedProperty(Object bean, String name, Object value)
PropertyUtils中等效于上面的Java代码将是这样:
[java]
view plaincopy
String city = (String) PropertyUtils.getNestedProperty(employee, "address(home).city");
最后,方便起见,PropertyUtils提供了如下一组方法,它们接收simple、indexed和mapped属性的任意组合方法,支持任意层次的嵌套:
PropertyUtils.getProperty(Object bean, String name)
PropertyUtils.setProperty(Object bean, String name, Object value)
你可以像这样使用:
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"));
}
输出:
id = 1 name = 断点
name = 每天进步一点
id = 1 name = 每天进步一点
原文链接:http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/package-summary.html#standard
一般情况下,在Java中你可以通过get方法轻松获取beans中的属性值。但是,当你事先不知道beans的类型或者将要访问或修改的属性名时,该怎么办?Java语言中提供了一些像
java.beans.Introspector这样类,实现了在运行时检测Java类并确定属性get和set方法的名称,结合Java中的反射机制就可以调用这些方法了。然而,这些APIs使用起来比较困难,并且将Java类中一些不必要的底层结构暴露给了开发人员。BeanUtils包中的APIs试图简化动态获取和设置bean属性的过程。
BeanUtils包中的PropertyUtils类中的一些静态方法实现了上面的功能,稍后会详细介绍。首先,介绍一些有用的定义:
JavaBean支持的属性类型一般可以划分成三类--标准的JavaBeans规范支持其中的一些,也有一部分只有BeanUtils包支持:
Simple(单值) -- 单值或量,有个一可以访问或修改的属性。值的类型可能是Java语言的原生类型(如:int型),简单的类(如:java.lang.String),或者一个复杂类的对象,这个类可能来自Java语言或者来自应用程序再或者来自应用程序中的一个类库。
Indexed(索引) -- 索引的属性,属性中存放有序对象(都是同类型的)的集合,每个对象都可以通过一个非负的整数值(或下标)来获取。另外,所有值的集合可以使用一个数组来设置或者获取。作为一个JavaBeans规范的扩展,BeanUtils包认为所有底层数据类型为java.util.List(或List的一个实现)的属性都可以被索引。
Mapped(映射) -- 作为一个标准JavaBeans APIs的扩展, BeanUtils包认为所有底层数据类型为java.util.Map的属性都可以被"映射"。你可以通过String类型的key值来设置或者获取对应的值。
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); }
访问基本属性
获取和设置simple属性很简单。在Javadocs中查看下面两个方法:
PropertyUtils.getSimpleProperty(Object bean, String name)
PropertyUtils.setSimpleProperty(Object bean, String name, Object value)
使用这两个方法,你可以动态地修改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);
对于indexed(索引)属性,你有两种选择 - 你既可以在属性名后面添加方括号在里面放上一个下标,也可以在调用方法时将其作为一个独立参数:
PropertyUtils.getIndexedProperty(Object bean, String name)
PropertyUtils.getIndexedProperty(Object bean, String name, int index)
PropertyUtils.setIndexedProperty(Object bean, String name, Object value)
PropertyUtils.setIndexedProperty(Object bean, String name, int index, Object value)
属性名的下标只能是整数常量。如果你想获取的项的索引是计算出来的,你可以将属性名和索引作为字符串组合起来。例如,你可以向下面这样做:
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);
类似的,获取和设置mapped(映射)属性的方法同样有两对。与indexed(索引)不同的是额外的属性是用括号括起来的(“(”和“)”)而不是方括号,并且获取和设置值时如同从底层的map中获取和设置值一样。
PropertyUtils.getMappedProperty(Object bean, String name)
PropertyUtils.getMappedProperty(Object bean, String name, String key)
PropertyUtils.setMappedProperty(Object bean, String name, Object value)
PropertyUtils.setMappedProperty(Object bean, String name, String key, Object value)
例如,你可以使用下面两种方法设置employee的家庭住址:
Employee employee = ...; Address address = ...; PropertyUtils.setMappedProperty(employee, "address(home)", address); Employee employee = ...; Address address = ...; PropertyUtils.setMappedProperty(employee, "address", "home", address);
访问嵌套属性
在上面的例子中,我们假设你将bean作为第一个参数传入PropertyUtils方法,并希望获取指定属性的值。然而,如果属性的值是一个Java对象,并且你希望进一步获取这个Java对象的某个属性的值?
例如,假设你事实上想要获取的值是employee家庭住址中的city属性。使用标准的Java编程技术直接获取bean的对应属性,你可以这样写:
[java]
view plaincopy
String city = employee.getAddress("home").getCity();
使用PropertyUtils类中的等效机制被称为嵌套属性访问。使用这种方法,你将访问路径上的属性的名称用“.”拼接起来 --与你在JavaScript执行嵌套属性访问的方式非常相似。
PropertyUtils.getNestedProperty(Object bean, String name)
PropertyUtils.setNestedProperty(Object bean, String name, Object value)
PropertyUtils中等效于上面的Java代码将是这样:
[java]
view plaincopy
String city = (String) PropertyUtils.getNestedProperty(employee, "address(home).city");
最后,方便起见,PropertyUtils提供了如下一组方法,它们接收simple、indexed和mapped属性的任意组合方法,支持任意层次的嵌套:
PropertyUtils.getProperty(Object bean, String name)
PropertyUtils.setProperty(Object bean, String name, Object value)
你可以像这样使用:
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"));
}
输出:
id = 1 name = 断点
name = 每天进步一点
id = 1 name = 每天进步一点
其它例子: import org.apache.commons.beanutils.PropertyUtils; List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); for(BaseGrpMemberVO member : MemberVO){ Map m = new HashMap(); for(Map.Entry<String, String> entry: fieldMap.entrySet()){ String key = entry.getKey(); fieldList.add(key); //记录字段名 String[] keyArray = key.split("\\."); if(keyArray.length == 2){//成员信息 Object o =PropertyUtils.getProperty(member, keyArray[1]); m.put(key, o==null?"":o.toString()); }else if(keyArray.length == 3){//险别信息 List<BaseGrpCvrgVO> cvrgVoList = mgr.getRelCvrgById(member.getCPkId()); String cvrgNo = keyArray[0]; for(BaseGrpCvrgVO cvrgVo : cvrgVoList){ if(cvrgNo.equals(cvrgVo.getCCvrgNo())){ Object o =PropertyUtils.getProperty(cvrgVo, keyArray[2]); m.put(key, o==null?"":o.toString()); } } } } data.add(m); }
原文链接:http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/package-summary.html#standard
相关文章推荐
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- Java PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- PropertyUtils.getProperty的用法
- BeanUtils.copyProperties 与 PropertyUtils.copyProperties 用法及区别
- STL之五:set/multiset用法详解
- List,Set,Map用法详解
- set_include_path用法详解
- SetBkMode函数用法详解
- STL之五:set/multiset用法详解
- java set hashset 详解 用法~~~
- mysql中find_in_set()函数的使用及in()用法详解
- response.setHeader各种用法详解(转)
- PropertyUtils.copyProperties()用法和性能
- set_include_path和get_include_path用法详解
- C++中set用法详解
- System.getProperty用法详解