- Java BeanUtils 教程
- Java BeanUtils - 主页
- Java BeanUtils - 概述
- 动态 Bean (DynaBeans)
- 背景
- 基本 DynaBeans
- 结果集动态类
- 行集动态类
- 包装DynaBean
- 惰性 DynaBeans
- 实用对象和类
- 实用对象和类
- Java BeanUtils 有用资源
- Java BeanUtils - 快速指南
- Java BeanUtils - 资源
- Java BeanUtils - 讨论
Java BeanUtils - 基本属性访问
描述
您可以通过以下方式访问基本属性:
简单属性
索引财产
映射属性
简单属性
您可以使用以下 API 签名获取和设置简单的属性值:
PropertyUtils.getSimpleProperty(对象,字符串)
PropertyUtils.SetSimpleProperty(对象,字符串,对象)
参数:
Object:是一个bean对象,指定要提取的bean属性。
String:它是一个字符串名称,指定要提取的属性的名称。
索引财产
您可以使用两个选项来创建索引属性;第一个选项是将下标构建到属性名称中,第二个选项是在单独的参数中定义下标以调用该方法。
可以使用以下方法获取和设置索引属性:
PropertyUtils.getIndexedProperty(对象,字符串)
PropertyUtils.getIndexedProperty(对象,字符串,int)
PropertyUtils.setIndexedProperty(对象,字符串,对象)
PropertyUtils.setIndexedProperty(对象,字符串,int,对象)
参数:
Object:是一个bean对象,指定要提取的bean属性。
String:它是一个字符串名称,指定要提取的属性的名称。
int:它设置属性值的索引。
Object:它指定索引属性元素的值。
映射属性
您可以使用以下 API 签名获取和设置映射的属性值。如果您有任何额外的参数,则可以将其写在括号内为(“(”和“)”),而不是使用方括号。
PropertyUtils.getMappedProperty(对象,字符串)
PropertyUtils.getMappedProperty(对象,字符串,字符串)
PropertyUtils.setMappedProperty(对象,字符串,对象)
PropertyUtils.setMappedProperty(对象,字符串,字符串,对象)
参数:
Object:是一个bean对象,指定要提取的bean属性。
String:它是应该为映射属性设置的属性值的名称。
String:定义要设置的属性值的键。
Object:指定要设置的属性值。
例子
下面的示例演示了在 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
@SuppressWarnings("rawtypes")
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; }
}
输出
让我们执行以下步骤来看看上面的代码是如何工作的:
将上面的第一个代码保存为BeanUtilsPropertyDemo.java。
现在使用“运行”选项或 Ctrl+f11 执行代码,并显示如下输出。
