日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【commons-beanutils专题】003- PropertyUtils 专题

發(fā)布時(shí)間:2024/8/1 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【commons-beanutils专题】003- PropertyUtils 专题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【commons-beanutils專題】003- PropertyUtils 專題

文章目錄

  • 【commons-beanutils專題】003- PropertyUtils 專題
  • 一、準(zhǔn)備
    • 0、PropertyUtils 主要作用
    • 1、引入 commons-beanutils 依賴
    • 2、pom.xml 文件
    • 3、實(shí)體類
    • 4、前置代碼
  • 二、暫時(shí)不知道有什么用的功能
    • 1、清空所有屬性的描述信息
    • 2、重置 BeanIntrospectors 的緩存屬性描述符信息
    • 3、添加一個(gè) BeanIntrospector
    • 4、移除一個(gè) BeanIntrospector
  • 三、淺拷貝
  • 四、獲取對象的屬性描述
  • 五、獲取屬性值
    • 1、指定索引屬性值,適用于屬性是 list 或 array 的情況
    • 2、指定索引屬性值,適用于屬性是 list 或 array 的情況
    • 3、獲取 map 屬性,適用于屬性是 map 的情況
    • 4、獲取 map 屬性,適用于屬性是 map 的情況
    • 5、獲取嵌套屬性,屬性是對象的情況
    • 6、獲取屬性
  • 六、獲取屬性描述
    • 1、獲取屬性描述 - 通過對象
    • 2、獲取屬性描述 - 通過類
    • 3、獲取屬性描述數(shù)組
  • 七、獲取屬性類型
    • 1、獲取已為此屬性注冊的任何顯式 PropertyEditor Class
    • 2、獲取屬性類型
  • 八、判斷屬性可讀可寫
    • 1、判斷一個(gè)屬性是否為可讀屬性
    • 2、判斷一個(gè)屬性是否為可寫屬性
  • 九、設(shè)置屬性值
    • 1、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況
    • 2、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況
    • 3、設(shè)置指定屬性值,適用于屬性是 map 的情況
    • 4、設(shè)置指定屬性值,適用于屬性是 map 的情況
    • 5、設(shè)置屬性值
    • 6、設(shè)置簡單屬性值
  • 十、完整代碼
  • 十一、常見錯(cuò)誤
    • 1、java.lang.NoSuchMethodException: Property 'name' has no getter method in class 'class com.zibo.zibo2022.main.Tag'
      • 答案:

一、準(zhǔn)備

0、PropertyUtils 主要作用

主要用于通過反射技術(shù)操作對象的屬性:獲取屬性、設(shè)置屬性、獲取屬性描述、淺拷貝等

1、引入 commons-beanutils 依賴

<!--引入依賴commons-beanutils--> <dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.4</version> </dependency>

2、pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zibo</groupId><artifactId>zibo2022</artifactId><version>0.0.1-SNAPSHOT</version><name>zibo2022</name><description>zibo2022</description><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入依賴commons-beanutils--><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.4</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.7.1</version><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

3、實(shí)體類

package com.zibo.zibo2022.property_utils.entity;import java.util.List; import java.util.Map; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;/*** @author zibo* @date 2022/7/14 0014 18:41*/ @Data @NoArgsConstructor @AllArgsConstructor public class Student {/*** 名字*/private String name;/*** 年齡*/private Integer age;/*** 地址*/private String address;/*** 朋友*/private Friend friend;@Data@NoArgsConstructor@AllArgsConstructorpublic static class Friend {private String name;private Integer age;private String address;}/*** 愛好*/private List<String> hobbies;/*** 學(xué)校*/private Map<String, String> schoolMap;}

4、前置代碼

Map<String, String> schoolMap = new HashMap<>(); schoolMap.put("高中", "某高中"); schoolMap.put("大學(xué)", "某大學(xué)"); Student student = new Student("訾博", 22, "北京", new Student.Friend("劉備", 26, "北京"), Arrays.asList("籃球", "足球"), schoolMap);

二、暫時(shí)不知道有什么用的功能

1、清空所有屬性的描述信息

// 1、清空所有屬性的描述信息 // 說明:清除任何類加載器加載的所有類的任何緩存屬性描述符信息。這在丟棄類加載器以實(shí)現(xiàn)類重新加載的情況下很有用。 PropertyUtils.clearDescriptors();

2、重置 BeanIntrospectors 的緩存屬性描述符信息

// 2、重置 BeanIntrospectors 的緩存屬性描述符信息 // 說明:將注冊的 BeanIntrospector 對象重置為初始默認(rèn)狀態(tài)。 PropertyUtils.resetBeanIntrospectors();

3、添加一個(gè) BeanIntrospector

// 3、添加一個(gè) BeanIntrospector // 說明:添加一個(gè) BeanIntrospector ,當(dāng)需要獲取類的屬性描述符時(shí)調(diào)用該對象。 PropertyUtils.addBeanIntrospector(DefaultBeanIntrospector.INSTANCE);

4、移除一個(gè) BeanIntrospector

// 4、移除一個(gè) BeanIntrospector // 說明:移除指定的 BeanIntrospector 。 BeanIntrospector instance = DefaultBeanIntrospector.INSTANCE; PropertyUtils.removeBeanIntrospector(instance);

三、淺拷貝

// 5、復(fù)制屬性 // 說明:經(jīng)測試這是一種淺拷貝! try {Student student1 = new Student();// 參數(shù):目標(biāo)對象,源對象PropertyUtils.copyProperties(student1, student);System.out.println(student);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學(xué)=某大學(xué), 高中=某高中})System.out.println(student1);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學(xué)=某大學(xué), 高中=某高中}) } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

四、獲取對象的屬性描述

// 6、獲取屬性描述 // 說明:經(jīng)測試發(fā)現(xiàn),含所有屬性和 class 屬性 try {Map<String, Object> map = PropertyUtils.describe(student);map.forEach((key, value) -> System.out.println(key + ":" + value));// address:北京// hobbies:[籃球, 足球]// friend:Student.Friend(name=劉備, age=26, address=北京)// name:訾博// schoolMap:{大學(xué)=某大學(xué), 高中=某高中}// class:class com.zibo.zibo2022.property_utils.entity.Student// age:22 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

五、獲取屬性值

1、指定索引屬性值,適用于屬性是 list 或 array 的情況

// 7、指定索引屬性值,適用于屬性是 list 或 array 的情況 try {Object property = PropertyUtils.getIndexedProperty(student, "hobbies[0]");System.out.println(property); // 籃球 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

2、指定索引屬性值,適用于屬性是 list 或 array 的情況

// 8、指定索引屬性值,適用于屬性是 list 或 array 的情況 try {Object property = PropertyUtils.getIndexedProperty(student, "hobbies", 1);System.out.println(property); // 足球 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

3、獲取 map 屬性,適用于屬性是 map 的情況

// 9、獲取 map 屬性,適用于屬性是 map 的情況 try {Object property = PropertyUtils.getMappedProperty(student, "schoolMap(高中)");System.out.println(property); // 某高中 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

4、獲取 map 屬性,適用于屬性是 map 的情況

// 10、獲取 map 屬性,適用于屬性是 map 的情況 try {Object property = PropertyUtils.getMappedProperty(student, "schoolMap", "大學(xué)");System.out.println(property); // 某大學(xué) } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

5、獲取嵌套屬性,屬性是對象的情況

// 11、獲取嵌套屬性,屬性是對象的情況 try {Object property = PropertyUtils.getNestedProperty(student, "friend.name");System.out.println(property); // 劉備 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

6、獲取屬性

// 12、獲取屬性 try {Object property = PropertyUtils.getProperty(student, "name");System.out.println(property); // 訾博 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

六、獲取屬性描述

1、獲取屬性描述 - 通過對象

// 13、獲取屬性描述 - 通過對象 try {PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(student, "name");System.out.println(propertyDescriptor);// 好強(qiáng)大呀!// java.beans.PropertyDescriptor[name=name; values={expert=false; visualUpdate=false; hidden=false;// enumerationValues=[Ljava.lang.Object;@204f30ec; required=false}; propertyType=class java.lang.String;// readMethod=public java.lang.String com.zibo.zibo2022.property_utils.entity.Student.getName();// writeMethod=public void com.zibo.zibo2022.property_utils.entity.Student.setName(java.lang.String)] } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

2、獲取屬性描述 - 通過類

// 14、獲取屬性描述 - 通過類 try {PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(Student.class, "name");System.out.println(propertyDescriptor);// java.beans.PropertyDescriptor[name=name; values={expert=false; visualUpdate=false; hidden=false;// enumerationValues=[Ljava.lang.Object;@527740a2; required=false}; propertyType=class java.lang.String;// readMethod=public java.lang.String java.lang.Class.getName()] } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

3、獲取屬性描述數(shù)組

// 15、獲取屬性描述數(shù)組 PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(student); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {System.out.println(propertyDescriptor); // 太多了,不寫了 }

七、獲取屬性類型

1、獲取已為此屬性注冊的任何顯式 PropertyEditor Class

// 16、獲取已為此屬性注冊的任何顯式 PropertyEditor Class try {Class<?> propertyEditorClass = PropertyUtils.getPropertyEditorClass(student, "name");System.out.println(propertyEditorClass); // 此處返回為 null ,具體不知道何用 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

2、獲取屬性類型

// 17、獲取屬性類型 try {Class<?> propertyType = PropertyUtils.getPropertyType(student, "name");System.out.println(propertyType); // class java.lang.String } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

八、判斷屬性可讀可寫

1、判斷一個(gè)屬性是否為可讀屬性

// 18、判斷一個(gè)屬性是否為可讀屬性 boolean isReadable = PropertyUtils.isReadable(student, "name"); System.out.println(isReadable); // true

2、判斷一個(gè)屬性是否為可寫屬性

// 19、判斷一個(gè)屬性是否為可寫屬性 boolean isWriteable = PropertyUtils.isWriteable(student, "name"); System.out.println(isWriteable); // true

九、設(shè)置屬性值

1、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況

// 20、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況 try {PropertyUtils.setIndexedProperty(student, "hobbies[0]", "da籃球");System.out.println(student.getHobbies()); // [da籃球, 足球] } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

2、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況

// 21、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況 try {PropertyUtils.setIndexedProperty(student, "hobbies", 1, "足da球");System.out.println(student.getHobbies()); // [da籃球, 足da球] } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

3、設(shè)置指定屬性值,適用于屬性是 map 的情況

// 22、設(shè)置指定屬性值,適用于屬性是 map 的情況 try {PropertyUtils.setMappedProperty(student, "schoolMap(高中)", "某da高中");System.out.println(student.getSchoolMap()); // {大學(xué)=某大學(xué), 高中=某da高中} } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

4、設(shè)置指定屬性值,適用于屬性是 map 的情況

// 23、設(shè)置指定屬性值,適用于屬性是 map 的情況 try {PropertyUtils.setMappedProperty(student, "schoolMap", "大學(xué)", "某da大學(xué)");System.out.println(student.getSchoolMap()); // {大學(xué)=某da大學(xué), 高中=某da高中} } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

5、設(shè)置屬性值

// 24、設(shè)置屬性值 try {PropertyUtils.setProperty(student, "name", "某da學(xué)生");System.out.println(student.getName()); // 某da學(xué)生 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

6、設(shè)置簡單屬性值

// 25、設(shè)置屬性值 try {PropertyUtils.setSimpleProperty(student, "name", "某da&da學(xué)生");System.out.println(student.getName()); // 某da學(xué)生 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }

十、完整代碼

package com.zibo.zibo2022.property_utils.main;import com.zibo.zibo2022.property_utils.entity.Student; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanIntrospector; import org.apache.commons.beanutils.DefaultBeanIntrospector; import org.apache.commons.beanutils.PropertyUtils;/*** @author zibo* @date 2022/7/14 0014 18:41*/ public class Main {public static void main(String[] args) {Map<String, String> schoolMap = new HashMap<>();schoolMap.put("高中", "某高中");schoolMap.put("大學(xué)", "某大學(xué)");Student student = new Student("訾博", 22, "北京", new Student.Friend("劉備", 26, "北京"), Arrays.asList("籃球", "足球"), schoolMap);// 看不懂,暫且擱置// 1、清空所有屬性的描述信息// 說明:清除任何類加載器加載的所有類的任何緩存屬性描述符信息。這在丟棄類加載器以實(shí)現(xiàn)類重新加載的情況下很有用。PropertyUtils.clearDescriptors();// 2、重置 BeanIntrospectors 的緩存屬性描述符信息// 說明:將注冊的 BeanIntrospector 對象重置為初始默認(rèn)狀態(tài)。PropertyUtils.resetBeanIntrospectors();// 3、添加一個(gè) BeanIntrospector// 說明:添加一個(gè) BeanIntrospector ,當(dāng)需要獲取類的屬性描述符時(shí)調(diào)用該對象。PropertyUtils.addBeanIntrospector(DefaultBeanIntrospector.INSTANCE);// 4、移除一個(gè) BeanIntrospector// 說明:移除指定的 BeanIntrospector 。BeanIntrospector instance = DefaultBeanIntrospector.INSTANCE;PropertyUtils.removeBeanIntrospector(instance);// 5、復(fù)制屬性// 說明:經(jīng)測試這是一種淺拷貝!try {Student student1 = new Student();// 參數(shù):目標(biāo)對象,源對象PropertyUtils.copyProperties(student1, student);System.out.println(student);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學(xué)=某大學(xué), 高中=某高中})System.out.println(student1);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學(xué)=某大學(xué), 高中=某高中})} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 6、獲取屬性描述// 說明:經(jīng)測試發(fā)現(xiàn),含所有屬性和 class 屬性try {Map<String, Object> map = PropertyUtils.describe(student);map.forEach((key, value) -> System.out.println(key + ":" + value));// address:北京// hobbies:[籃球, 足球]// friend:Student.Friend(name=劉備, age=26, address=北京)// name:訾博// schoolMap:{大學(xué)=某大學(xué), 高中=某高中}// class:class com.zibo.zibo2022.property_utils.entity.Student// age:22} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 7、指定索引屬性值,適用于屬性是 list 或 array 的情況try {Object property = PropertyUtils.getIndexedProperty(student, "hobbies[0]");System.out.println(property); // 籃球} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 8、指定索引屬性值,適用于屬性是 list 或 array 的情況try {Object property = PropertyUtils.getIndexedProperty(student, "hobbies", 1);System.out.println(property); // 足球} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 9、獲取 map 屬性,適用于屬性是 map 的情況try {Object property = PropertyUtils.getMappedProperty(student, "schoolMap(高中)");System.out.println(property); // 某高中} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 10、獲取 map 屬性,適用于屬性是 map 的情況try {Object property = PropertyUtils.getMappedProperty(student, "schoolMap", "大學(xué)");System.out.println(property); // 某大學(xué)} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 11、獲取嵌套屬性,屬性是對象的情況try {Object property = PropertyUtils.getNestedProperty(student, "friend.name");System.out.println(property); // 劉備} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 12、獲取屬性try {Object property = PropertyUtils.getProperty(student, "name");System.out.println(property); // 訾博} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 13、獲取屬性描述 - 通過對象try {PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(student, "name");System.out.println(propertyDescriptor);// 好強(qiáng)大呀!// java.beans.PropertyDescriptor[name=name; values={expert=false; visualUpdate=false; hidden=false;// enumerationValues=[Ljava.lang.Object;@204f30ec; required=false}; propertyType=class java.lang.String;// readMethod=public java.lang.String com.zibo.zibo2022.property_utils.entity.Student.getName();// writeMethod=public void com.zibo.zibo2022.property_utils.entity.Student.setName(java.lang.String)]} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 14、獲取屬性描述 - 通過類try {PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(Student.class, "name");System.out.println(propertyDescriptor);// java.beans.PropertyDescriptor[name=name; values={expert=false; visualUpdate=false; hidden=false;// enumerationValues=[Ljava.lang.Object;@527740a2; required=false}; propertyType=class java.lang.String;// readMethod=public java.lang.String java.lang.Class.getName()]} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 15、獲取屬性描述數(shù)組PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(student);for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {System.out.println(propertyDescriptor); // 太多了,不寫了}// 16、獲取已為此屬性注冊的任何顯式 PropertyEditor Classtry {Class<?> propertyEditorClass = PropertyUtils.getPropertyEditorClass(student, "name");System.out.println(propertyEditorClass); // 此處返回為 null ,具體不知道何用} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 17、獲取屬性類型try {Class<?> propertyType = PropertyUtils.getPropertyType(student, "name");System.out.println(propertyType); // class java.lang.String} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 18、判斷一個(gè)屬性是否為可讀屬性boolean isReadable = PropertyUtils.isReadable(student, "name");System.out.println(isReadable); // true// 19、判斷一個(gè)屬性是否為可寫屬性boolean isWriteable = PropertyUtils.isWriteable(student, "name");System.out.println(isWriteable); // true// 20、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況try {PropertyUtils.setIndexedProperty(student, "hobbies[0]", "da籃球");System.out.println(student.getHobbies()); // [da籃球, 足球]} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 21、設(shè)置指定索引屬性值,適用于屬性是list或者array的情況try {PropertyUtils.setIndexedProperty(student, "hobbies", 1, "足da球");System.out.println(student.getHobbies()); // [da籃球, 足da球]} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 22、設(shè)置指定屬性值,適用于屬性是 map 的情況try {PropertyUtils.setMappedProperty(student, "schoolMap(高中)", "某da高中");System.out.println(student.getSchoolMap()); // {大學(xué)=某大學(xué), 高中=某da高中}} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 23、設(shè)置指定屬性值,適用于屬性是 map 的情況try {PropertyUtils.setMappedProperty(student, "schoolMap", "大學(xué)", "某da大學(xué)");System.out.println(student.getSchoolMap()); // {大學(xué)=某da大學(xué), 高中=某da高中}} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 24、設(shè)置屬性值try {PropertyUtils.setProperty(student, "name", "某da學(xué)生");System.out.println(student.getName()); // 某da學(xué)生} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 25、設(shè)置屬性值try {PropertyUtils.setSimpleProperty(student, "name", "某da&da學(xué)生");System.out.println(student.getName()); // 某da學(xué)生} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}}}

十一、常見錯(cuò)誤

1、java.lang.NoSuchMethodException: Property ‘name’ has no getter method in class ‘class com.zibo.zibo2022.main.Tag’

答案:

用BeanUtils.getProperty獲取屬性的類必須是public,否則會報(bào)此錯(cuò)誤!

總結(jié)

以上是生活随笔為你收集整理的【commons-beanutils专题】003- PropertyUtils 专题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。