what happens in more sophisticated environments where you do not necessarily know ahead of time which bean class
you are going to be using, or which property you want to retrieve or modify?
The APIs in the BeanUtils package are intended to simplify getting and setting bean properties dynamically, where the
objects you are accessing -- and the names of the properties you care about -- are determined at runtime in your
application, rather than as you are writing and compiling your application's classes.
1.getSimpleProperty()读取属性
1 Person person=new Person(); 2 person.setName=("heis"); 3 String name=(String)PropertyUtils.getSimpleProperty(person,"name");
2.getNestedProperty()检索嵌套的bean属性
1 Book book=new Book(); 2 book.setAuthor(person); 3 String authorName=(String)PropertyUtils.getNestedProperty(book,"author.name");
3. getIndexedProperty()访问数组或List型内Object的属性
1 Chapter chapter1=new Chapter(); 2 Chapter chapter2=new Chapter(); 3 book.getChapters().add(chapter1); 4 book.getChapters().add(chapter2); 5 Chapter chapter=(Chapter)PropertyUtils.getIndexedProperty(book,"chapter[0]");
4.getMappedProperty()访问Map型bean属性的值
1 Person person=new Person(); 2 person.setName=("heis"); 3 Map favorites=new HashMap(); 4 favorites.put("food","rice"); 5 person.setFavorite(favorites); 6 String favorFood=(String)PropertyUtils.getMappedProperty(person,"favorites(food)");
5.getProperty()和setProperty()可以访问任何bean属性,通过表达式可以完成上面方法的功能
1 Book book 2 |--List authors 3 |--[0]->Person person 4 |--Map favorites 5 |--Entry(key->"food",value->"") 6 PropertyUtils.setProperty(book,"authors[0].favorites(food)","rice"); 7 String favorFood=(String)PropertyUtils.getProperty(book,"authors[0].favorites(food)");