标签:
Apache BeanUtils 是 Apache开源软件组织下面的一个项目,被广泛使用于Spring、Struts、Hibernate等框架,有数千个jar包依赖于它。它通过JDK中反射和自省
的功能,提供了许多实用但JDK并未直接提供的功能。我找到了官方的入门文档,用自己的语言翻译出来,希望大家指正。
最早可能要从JavaBean说起,这个名称来源于一个针对组件架构的Java API,按照JavaBeans设计原则来编写Java类会让开发者更容易理解你的类所能提供的功能,就好像允许那些能够意
JavaBeans的工具来使用Java的内省能力来知道你的类所提供的的属性和操作。并用一种具有视觉吸引力的方式展现在开发工具上(我的理解就是,在用Eclipse或IntelliJ idea时在对象名后面按小数点后会
弹出方法列表 )。
JavaBeans规范定义了完整的特性集合来判断任意一个Java类是否是JavaBean,你应该考虑把阅读这个文档作为你Java编程技能的重要部分。部分重要特性如下:
public class Employee { public Employee(); // Zero-arguments constructor public String getFirstName(); public void setFirstName(String firstName); public String getLastName(); public void setLastName(String lastName); public Date getHireDate(); public void setHireDate(Date hireDate); public boolean isManager(); public void setManager(boolean manager); public String getFullName(); }
Employee employee = ...;
System.out.println("Hello " + employee.getFirstName() + "!");
下面用一个自己写的例子来入门。粘贴进编辑器直接运行。
package beanUtils; import java.util.HashMap; import java.util.Map; /** * Created by Andrew on 2015/12/4. */ public class Employee { String firstName; String lastName; Employee[] subordinate; Map<String, Address> address; public Employee(){ firstName = "Adnrew"; lastName = "Chen"; subordinate = new Employee[]{new Employee("Shirley","Liu"),new Employee("Alex","Wang")}; address = new HashMap<>(); address.put("home", new Address("Changsha YueLuShan")); } private Employee(String firstName,String lastName){ this.firstName = firstName; this.lastName = lastName; } public Address getAddress(String type) { return address.get(type); } public void setAddress(String type, Address address) { this.address.put(type, address); } public Employee getSubordinate(int index) { return subordinate[index]; } public void setSubordinate(int index, Employee subordinate) { this.subordinate[index] = subordinate; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; }public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return firstName+" "+lastName; } }
package beanUtils; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.InvocationTargetException; /** * Created by Andrew on 2015/12/4. */ public class BeanUtilsTest { public static void main(String[] args) { Employee employee = new Employee(); try { System.out.println((String) PropertyUtils.getSimpleProperty(employee, "firstName")); System.out.println((String)PropertyUtils.getSimpleProperty(employee, "lastName")); System.out.println(PropertyUtils.getIndexedProperty(employee, "subordinate[0]")); System.out.println(PropertyUtils.getMappedProperty(employee, "address(home)")); System.out.println(PropertyUtils.getNestedProperty(employee, "address(home).city")); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
对于Simple属性,直接调用PropertyUtils.getSimpleProperty(employee, "firstName")就可以获得。
对于Indexed属性,PropertyUtils.getIndexedProperty(employee, "subordinate[0]")或是PropertyUtils.getIndexedProperty(employee, "subordinate",0)来获得。
对于Mapped属性,PropertyUtils.getMappedProperty(employee, "address(home)")PropertyUtils.getMappedProperty(employee, "address","home")来获得。
对于更复杂的嵌套属性,假设获取employee的address中key="home"的对象的city属性。我们可以用标准的写法:
String city = employee.getAddress("home").getCity();
通过PropertyUtils,你可以像JavaScript那样通过分隔符 "." 来取得嵌套的属性
String city = (String) PropertyUtils.getNestedProperty(employee, "address(home).city");
更详细的可以查看文档API。
之后会更新关于动态Bean的入门。
标签:
原文地址:http://www.cnblogs.com/andrew-chen/p/5019061.html