• BeanUtils 学习教程


        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)");
  • 相关阅读:
    poj 3278 catch that cow
    POJ 1028 Web Navigation
    poj 2643 election
    hdu 1908 double queues
    hdu_2669 Romantic(扩展欧几里得)
    0/1背包 dp学习~6
    校验码
    最长上升子序列(LIS经典变型) dp学习~5
    LCS最长公共子序列~dp学习~4
    最长上升子序列(LIS) dp学习~3
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3231661.html
Copyright © 2020-2023  润新知