Java Beanutils 简明教程
Java BeanUtils - Basic Property Access
Description
你可以通过以下方式访问基本属性:
-
Simple Property
-
Indexed Property
-
Mapped Property
Simple Property
你可以使用以下 API 签名来获取和设置 simple 属性值:
-
PropertyUtils.getSimpleProperty(Object, String)
-
PropertyUtils.SetSimpleProperty(Object, String, Object)
参数:
-
Object :这是一个 Bean 对象,指定要提取的 Bean 属性。
-
String :这是一个字符串名称,指定要提取的属性的名称。
Indexed Property
你可以使用两种选项来创建 indexed 属性;第一种选项是将下标构建到属性名中,第二种选项是在单独的参数中定义下标以调用该方法。
可以使用以下方法获取和设置经过索引的属性:
-
PropertyUtils.getIndexedProperty(Object, String)
-
PropertyUtils.getIndexedProperty(Object, String, int)
-
PropertyUtils.setIndexedProperty(Object, String, Object)
-
PropertyUtils.setIndexedProperty(Object, String, int, Object)
参数:
-
Object :这是一个 Bean 对象,指定要提取的 Bean 属性。
-
String :这是一个字符串名称,指定要提取的属性的名称。
-
int :设置属性值的下标。
-
Object :指定经过索引的属性元素的值。
Mapped Property
你可以使用以下 API 签名来获取和设置 mapped 属性。如果你有任何额外参数,则可以在括号内输入(“(”和“))”,而不是使用方括号。
-
PropertyUtils.getMappedProperty(Object, String)
-
PropertyUtils.getMappedProperty(Object, String, String)
-
PropertyUtils.setMappedProperty(Object, String, Object)
-
PropertyUtils.setMappedProperty(Object, String, String, Object)
参数:
-
Object :这是一个 Bean 对象,指定要提取的 Bean 属性。
-
String :它是应该为映射的属性设置的属性值。
-
String :定义要设置的属性值的关键。
-
Object :指定要设置的属性的值。
Example
以下示例演示了 beanUtils 中以上属性的使用:
import org.apache.commons.beanutils.PropertyUtils;
import java.util.ArrayList;
import java.util.List;
public class BeanUtilsPropertyDemo{
public static void main(String args[]){
try{
// Creating the bean and allows to access getter and setter properties
MyBean myBean = new MyBean();
// Setting the properties on the myBean
PropertyUtils.setSimpleProperty(myBean, "stringProp", "Hello!This is a string");
PropertyUtils.setSimpleProperty(myBean, "floatProp", new Float(25.20));
// Getting the simple properties
System.out.println("String Property: " + PropertyUtils.getSimpleProperty(myBean, "stringProp"));
System.out.println("Float Property: " + PropertyUtils.getSimpleProperty(myBean, "floatProp"));
// Here we will create a list for the indexed property
List list = new ArrayList();
list.add("String value 0");
list.add("String value 1");
myBean.setListProp(list);
// get and set this indexed property
PropertyUtils.setIndexedProperty(myBean, "listProp[1]", "This is new string value 1");
System.out.println("List Property[1]: " + PropertyUtils.getIndexedProperty(myBean, "listProp[1]"));
}catch(Exception e){
System.out.println(e);
}
}
}
现在,我们将为 bean 类创建另一个名为 MyBean.java 的类:
import java.util.ArrayList;
import java.util.List;
public class MyBean {
private String stringProp;
private float floatProp;
//indexed property
private List listProp = new ArrayList();
public void setStringProp(String stringProp) { this.stringProp = stringProp; }
public String getStringProp() { return this.stringProp; }
public void setFloatProp(float floatProp) { this.floatProp = floatProp; }
public float getFloatProp() { return this.floatProp; }
public void setListProp(List<?> listProp) { this.listProp = listProp; }
public List<?> getListProp() { return this.listProp; }
}