Java Beanutils 简明教程

Java BeanUtils - Basic Property Access

Description

你可以通过以下方式访问基本属性:

  1. Simple Property

  2. Indexed Property

  3. Mapped Property

Simple Property

你可以使用以下 API 签名来获取和设置 simple 属性值:

  1. PropertyUtils.getSimpleProperty(Object, String)

  2. PropertyUtils.SetSimpleProperty(Object, String, Object)

参数:

  1. Object :这是一个 Bean 对象,指定要提取的 Bean 属性。

  2. String :这是一个字符串名称,指定要提取的属性的名称。

Indexed Property

你可以使用两种选项来创建 indexed 属性;第一种选项是将下标构建到属性名中,第二种选项是在单独的参数中定义下标以调用该方法。

可以使用以下方法获取和设置经过索引的属性:

  1. PropertyUtils.getIndexedProperty(Object, String)

  2. PropertyUtils.getIndexedProperty(Object, String, int)

  3. PropertyUtils.setIndexedProperty(Object, String, Object)

  4. PropertyUtils.setIndexedProperty(Object, String, int, Object)

参数:

  1. Object :这是一个 Bean 对象,指定要提取的 Bean 属性。

  2. String :这是一个字符串名称,指定要提取的属性的名称。

  3. int :设置属性值的下标。

  4. Object :指定经过索引的属性元素的值。

Mapped Property

你可以使用以下 API 签名来获取和设置 mapped 属性。如果你有任何额外参数,则可以在括号内输入(“(”和“))”,而不是使用方括号。

  1. PropertyUtils.getMappedProperty(Object, String)

  2. PropertyUtils.getMappedProperty(Object, String, String)

  3. PropertyUtils.setMappedProperty(Object, String, Object)

  4. PropertyUtils.setMappedProperty(Object, String, String, Object)

参数:

  1. Object :这是一个 Bean 对象,指定要提取的 Bean 属性。

  2. String :它是应该为映射的属性设置的属性值。

  3. String :定义要设置的属性值的关键。

  4. 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; }
	}

Output

让我们执行以下步骤来了解上述代码的工作原理:

  1. 将以上第一个代码保存为 BeanUtilsPropertyDemo.java。

  2. 现在,使用“运行”选项或 Ctrl+f11 执行代码,将显示以下输出。

basic property access