zoukankan      html  css  js  c++  java
  • 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)");
  • 相关阅读:
    关于cmake、make、make install
    windows开启ip_forwarding功能
    最新devstack安装(ussuri)
    【rabbitmq】之业务封装
    【rabbitmq】之过期和死信队列
    【rabbitmq】之confirm和return机制
    【rabbitmq】之消费端手动ack
    java短网址服务
    详解druid打印SQL日志
    logback配置文件拆分,抽取公共配置
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3231661.html
Copyright © 2011-2022 走看看