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)");
  • 相关阅读:
    带你封装自己的『权限管理』框架
    一夜搞懂 | JVM 线程安全与锁优化
    一夜搞懂 | Java 内存模型与线程
    一夜搞懂 | JVM 字节码执行引擎
    一夜搞懂 | JVM 类加载机制
    一夜搞懂 | JVM GC&内存分配
    一文洞悉JVM内存管理机制
    Redis 的基本数据类型 和 基础应用场景
    MyISAM 和 InnoDB 索引结构及其实现原理
    一次性搞懂 PHP 中面向对象的所有知识点。
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3231661.html
Copyright © 2011-2022 走看看