【commons-beanutils专题】003- PropertyUtils 专题
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【commons-beanutils专题】003- PropertyUtils 专题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                【commons-beanutils專題】003- PropertyUtils 專題
文章目錄
- 【commons-beanutils專題】003- PropertyUtils 專題
- 一、準備
- 0、PropertyUtils 主要作用
- 1、引入 commons-beanutils 依賴
- 2、pom.xml 文件
- 3、實體類
- 4、前置代碼
 
- 二、暫時不知道有什么用的功能
- 1、清空所有屬性的描述信息
- 2、重置 BeanIntrospectors 的緩存屬性描述符信息
- 3、添加一個 BeanIntrospector
- 4、移除一個 BeanIntrospector
 
- 三、淺拷貝
- 四、獲取對象的屬性描述
- 五、獲取屬性值
- 1、指定索引屬性值,適用于屬性是 list 或 array 的情況
- 2、指定索引屬性值,適用于屬性是 list 或 array 的情況
- 3、獲取 map 屬性,適用于屬性是 map 的情況
- 4、獲取 map 屬性,適用于屬性是 map 的情況
- 5、獲取嵌套屬性,屬性是對象的情況
- 6、獲取屬性
 
- 六、獲取屬性描述
- 1、獲取屬性描述 - 通過對象
- 2、獲取屬性描述 - 通過類
- 3、獲取屬性描述數組
 
- 七、獲取屬性類型
- 1、獲取已為此屬性注冊的任何顯式 PropertyEditor Class
- 2、獲取屬性類型
 
- 八、判斷屬性可讀可寫
- 1、判斷一個屬性是否為可讀屬性
- 2、判斷一個屬性是否為可寫屬性
 
- 九、設置屬性值
- 1、設置指定索引屬性值,適用于屬性是list或者array的情況
- 2、設置指定索引屬性值,適用于屬性是list或者array的情況
- 3、設置指定屬性值,適用于屬性是 map 的情況
- 4、設置指定屬性值,適用于屬性是 map 的情況
- 5、設置屬性值
- 6、設置簡單屬性值
 
- 十、完整代碼
- 十一、常見錯誤
- 1、java.lang.NoSuchMethodException: Property 'name' has no getter method in class 'class com.zibo.zibo2022.main.Tag'
- 答案:
 
 
一、準備
0、PropertyUtils 主要作用
主要用于通過反射技術操作對象的屬性:獲取屬性、設置屬性、獲取屬性描述、淺拷貝等。
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、實體類
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;/*** 學校*/private Map<String, String> schoolMap;}4、前置代碼
Map<String, String> schoolMap = new HashMap<>(); schoolMap.put("高中", "某高中"); schoolMap.put("大學", "某大學"); Student student = new Student("訾博", 22, "北京", new Student.Friend("劉備", 26, "北京"), Arrays.asList("籃球", "足球"), schoolMap);二、暫時不知道有什么用的功能
1、清空所有屬性的描述信息
// 1、清空所有屬性的描述信息 // 說明:清除任何類加載器加載的所有類的任何緩存屬性描述符信息。這在丟棄類加載器以實現類重新加載的情況下很有用。 PropertyUtils.clearDescriptors();2、重置 BeanIntrospectors 的緩存屬性描述符信息
// 2、重置 BeanIntrospectors 的緩存屬性描述符信息 // 說明:將注冊的 BeanIntrospector 對象重置為初始默認狀態。 PropertyUtils.resetBeanIntrospectors();3、添加一個 BeanIntrospector
// 3、添加一個 BeanIntrospector // 說明:添加一個 BeanIntrospector ,當需要獲取類的屬性描述符時調用該對象。 PropertyUtils.addBeanIntrospector(DefaultBeanIntrospector.INSTANCE);4、移除一個 BeanIntrospector
// 4、移除一個 BeanIntrospector // 說明:移除指定的 BeanIntrospector 。 BeanIntrospector instance = DefaultBeanIntrospector.INSTANCE; PropertyUtils.removeBeanIntrospector(instance);三、淺拷貝
// 5、復制屬性 // 說明:經測試這是一種淺拷貝! try {Student student1 = new Student();// 參數:目標對象,源對象PropertyUtils.copyProperties(student1, student);System.out.println(student);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學=某大學, 高中=某高中})System.out.println(student1);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學=某大學, 高中=某高中}) } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }四、獲取對象的屬性描述
// 6、獲取屬性描述 // 說明:經測試發現,含所有屬性和 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:{大學=某大學, 高中=某高中}// 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", "大學");System.out.println(property); // 某大學 } 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);// 好強大呀!// 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、獲取屬性描述數組
// 15、獲取屬性描述數組 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、判斷一個屬性是否為可讀屬性
// 18、判斷一個屬性是否為可讀屬性 boolean isReadable = PropertyUtils.isReadable(student, "name"); System.out.println(isReadable); // true2、判斷一個屬性是否為可寫屬性
// 19、判斷一個屬性是否為可寫屬性 boolean isWriteable = PropertyUtils.isWriteable(student, "name"); System.out.println(isWriteable); // true九、設置屬性值
1、設置指定索引屬性值,適用于屬性是list或者array的情況
// 20、設置指定索引屬性值,適用于屬性是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、設置指定索引屬性值,適用于屬性是list或者array的情況
// 21、設置指定索引屬性值,適用于屬性是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、設置指定屬性值,適用于屬性是 map 的情況
// 22、設置指定屬性值,適用于屬性是 map 的情況 try {PropertyUtils.setMappedProperty(student, "schoolMap(高中)", "某da高中");System.out.println(student.getSchoolMap()); // {大學=某大學, 高中=某da高中} } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }4、設置指定屬性值,適用于屬性是 map 的情況
// 23、設置指定屬性值,適用于屬性是 map 的情況 try {PropertyUtils.setMappedProperty(student, "schoolMap", "大學", "某da大學");System.out.println(student.getSchoolMap()); // {大學=某da大學, 高中=某da高中} } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }5、設置屬性值
// 24、設置屬性值 try {PropertyUtils.setProperty(student, "name", "某da學生");System.out.println(student.getName()); // 某da學生 } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e); }6、設置簡單屬性值
// 25、設置屬性值 try {PropertyUtils.setSimpleProperty(student, "name", "某da&da學生");System.out.println(student.getName()); // 某da學生 } 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("大學", "某大學");Student student = new Student("訾博", 22, "北京", new Student.Friend("劉備", 26, "北京"), Arrays.asList("籃球", "足球"), schoolMap);// 看不懂,暫且擱置// 1、清空所有屬性的描述信息// 說明:清除任何類加載器加載的所有類的任何緩存屬性描述符信息。這在丟棄類加載器以實現類重新加載的情況下很有用。PropertyUtils.clearDescriptors();// 2、重置 BeanIntrospectors 的緩存屬性描述符信息// 說明:將注冊的 BeanIntrospector 對象重置為初始默認狀態。PropertyUtils.resetBeanIntrospectors();// 3、添加一個 BeanIntrospector// 說明:添加一個 BeanIntrospector ,當需要獲取類的屬性描述符時調用該對象。PropertyUtils.addBeanIntrospector(DefaultBeanIntrospector.INSTANCE);// 4、移除一個 BeanIntrospector// 說明:移除指定的 BeanIntrospector 。BeanIntrospector instance = DefaultBeanIntrospector.INSTANCE;PropertyUtils.removeBeanIntrospector(instance);// 5、復制屬性// 說明:經測試這是一種淺拷貝!try {Student student1 = new Student();// 參數:目標對象,源對象PropertyUtils.copyProperties(student1, student);System.out.println(student);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學=某大學, 高中=某高中})System.out.println(student1);// Student(name=訾博, age=22, address=北京, friend=Student.Friend(name=劉備, age=26, address=北京), hobbies=[籃球, 足球], schoolMap={大學=某大學, 高中=某高中})} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 6、獲取屬性描述// 說明:經測試發現,含所有屬性和 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:{大學=某大學, 高中=某高中}// 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", "大學");System.out.println(property); // 某大學} 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);// 好強大呀!// 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、獲取屬性描述數組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、判斷一個屬性是否為可讀屬性boolean isReadable = PropertyUtils.isReadable(student, "name");System.out.println(isReadable); // true// 19、判斷一個屬性是否為可寫屬性boolean isWriteable = PropertyUtils.isWriteable(student, "name");System.out.println(isWriteable); // true// 20、設置指定索引屬性值,適用于屬性是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、設置指定索引屬性值,適用于屬性是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、設置指定屬性值,適用于屬性是 map 的情況try {PropertyUtils.setMappedProperty(student, "schoolMap(高中)", "某da高中");System.out.println(student.getSchoolMap()); // {大學=某大學, 高中=某da高中}} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 23、設置指定屬性值,適用于屬性是 map 的情況try {PropertyUtils.setMappedProperty(student, "schoolMap", "大學", "某da大學");System.out.println(student.getSchoolMap()); // {大學=某da大學, 高中=某da高中}} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 24、設置屬性值try {PropertyUtils.setProperty(student, "name", "某da學生");System.out.println(student.getName()); // 某da學生} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}// 25、設置屬性值try {PropertyUtils.setSimpleProperty(student, "name", "某da&da學生");System.out.println(student.getName()); // 某da學生} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {throw new RuntimeException(e);}}}十一、常見錯誤
1、java.lang.NoSuchMethodException: Property ‘name’ has no getter method in class ‘class com.zibo.zibo2022.main.Tag’
答案:
用BeanUtils.getProperty獲取屬性的類必須是public,否則會報此錯誤!
總結
以上是生活随笔為你收集整理的【commons-beanutils专题】003- PropertyUtils 专题的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Windows Server 2016安
- 下一篇: 解决 Win11 开机ctfmon.ex
