淺談Java內(nèi)省機(jī)制
概念
JavaBean
在實(shí)際編程中,我們常常需要一些用來包裝值對象的類,例如Student、 Employee、Order,這些 類中往往沒有業(yè)務(wù)方法,只是為了把需要處理的實(shí)體對象進(jìn)行封裝,有這樣的特征:
- 屬性都是私有的;
- 有無參的public構(gòu)造方法;
- 對私有屬性根據(jù)需要提供公有的getXxx方法以及setXxx方法;
比如:屬性名稱為name,則有g(shù)etName方法返回屬性name值, setName方法設(shè)置name值;注意方法的名稱通常是get或 set加上屬性名稱,并把屬性名稱的首字母大寫;這些方法稱為getters/setters;getters必須有返回值沒有方法參數(shù); setter值沒有返回值,有方法參數(shù);
例如下面的例子:

符合這些特征的類,被稱為JavaBean;
內(nèi)省
內(nèi)省(Inspector)機(jī)制就是基于反射的基礎(chǔ), Java語言對Bean類屬性、事件的一種缺省處理方法。
只要類中有g(shù)etXXX方法,或者setXXX方法,或者同時(shí)有g(shù)etXXX及setXXX方法,其中g(shù)etXXX方 法沒有方法參數(shù),有返回值; setXXX方法沒有返回值,有一個(gè)方法參數(shù);那么內(nèi)省機(jī)制就認(rèn)為 XXX為一個(gè)屬性;
例如下面代碼
Employee類中根本沒有聲明age屬性,僅僅是聲明了這樣的getter和setter.內(nèi)省機(jī)制就認(rèn)為age是屬性
package com.shixun.introspector;
public class Employee {
private String name;
private Double score;
// age將被內(nèi)省認(rèn)為是屬性
public int getAge(){
return 30;
}
// name將被內(nèi)省認(rèn)為是屬性
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// score將被內(nèi)省認(rèn)為是屬性
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public static void main(String[] args) {
}
}
相關(guān)API
與Java內(nèi)省有關(guān)的主要類及接口有:
java.beans.Introspector類: 為獲得JavaBean屬性、事件、方法提供了標(biāo)準(zhǔn)方法;通常使用其中的getBeanInfo方法返回BeanInfo對象;Java.beans.BeanInfo接口:不能直接實(shí)例化,通常通過Introspector類返回該類型對象,提供了返回屬性描述符對象(PropertyDescriptor)、方法描述符對象(MethodDescriptor) 、 bean描述符(BeanDescriptor)對象的方法;Java.beans.PropertyDescriptor類:用來描述一個(gè)屬性,該屬性有g(shù)etter及setter方法;
可以使用PropertyDescriptor類的方法獲取屬性相關(guān)的信息,例如getName方法返回屬性的名字:
PropertyDescriptor類中定義了方法可以獲取該屬性的getter和setter方法
| 方法 | 方法描述 |
|---|---|
| Method getReadMethod() | 回屬性對應(yīng)的getter方法對象; |
| Method getWriteMethod() | 回屬性對應(yīng)的setter方法對象; |
下面我們來用代碼深入探究一下:
代碼案例:獲取屬性相關(guān)信息
Employee如上面代碼所示,繼續(xù)編寫主函數(shù)進(jìn)行測試
首先用BeanInfo接口獲取BeanInfo的對象,再通過BeanInfo對象獲取PropertyDescriptor屬性描述
//獲取BeanInfo的對象
BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);
//通過BeanInfo對象獲取PropertyDescriptor屬性描述
PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
System.out.println("通過Inspector內(nèi)省機(jī)制獲取JavaBean屬性======= 打印所有信息 ====================");
Arrays.stream(propertyDescriptors).forEach(f->{
System.out.println("====================================");
System.out.println("屬性名:"+f.getName());
System.out.println("類型:"+f.getPropertyType());
System.out.println("get方法:"+f.getReadMethod());
System.out.println("set方法:"+f.getWriteMethod());
});
// 或者用增強(qiáng)for
System.out.println("通過Inspector內(nèi)省機(jī)制獲取JavaBean屬性======= 打印所有信息 ====================");
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
System.out.println("====================================");
System.out.println("名字:" + propertyDescriptor.getName());
System.out.println("類型:" + propertyDescriptor.getPropertyType());
System.out.println("get方法:" + propertyDescriptor.getReadMethod());
System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
}
運(yùn)行結(jié)果如下:

我們也可以通過反射調(diào)用這里獲取的get或set方法
//創(chuàng)建Employee的對象
Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
Object employee = clazz.newInstance();
//遍歷屬性描述對象
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
//打印屬性名稱
System.out.println(propertyDescriptor.getName());
//判斷屬性名稱是不是name
if (propertyDescriptor.getName().equals("name")) {
//setter方法
Method writeMethod = propertyDescriptor.getWriteMethod();
//調(diào)用setName方法
writeMethod.invoke(employee, "jack");
//getter方法
Method readMethod = propertyDescriptor.getReadMethod();
//調(diào)用getName方法
Object nameValue = readMethod.invoke(employee);
System.out.println("name屬性的值為:" + nameValue);
}
//判斷屬性名稱是否為score
if (propertyDescriptor.getName().equals("score")) {
//setter方法
Method scoreWriteMethod = propertyDescriptor.getWriteMethod();
//調(diào)用setScore方法
scoreWriteMethod.invoke(employee, new Double(3000));
//getter方法
Method scoreReadMethod = propertyDescriptor.getReadMethod();
Object scoreValue = scoreReadMethod.invoke(employee);
System.out.println("score屬性的值為:" + scoreValue);
}
}
System.out.println("當(dāng)前對象的信息:"+employee.toString());
運(yùn)行結(jié)果如下所示:

全部代碼附在最下方?。。。。?!
內(nèi)省屬性的注意事項(xiàng)
- 很多框架都使用了內(nèi)省機(jī)制檢索對象的屬性,定義屬性名字時(shí),名字最好起碼以兩個(gè)小寫字母開頭,例如stuName,而不要使用sName,某些情況下,可能會導(dǎo)致檢索屬性失??;
- 內(nèi)省機(jī)制檢索屬性時(shí),是根據(jù)getter和setter方法確認(rèn)屬性名字,而不是根據(jù)類里聲明的成員變量名稱決定;
完整代碼
package com.shixun.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Employee {
private String name;
private Double score;
// age將被內(nèi)省認(rèn)為是屬性
public int getAge() {
return 30;
}
// name將被內(nèi)省認(rèn)為是屬性
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// score將被內(nèi)省認(rèn)為是屬性
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", score=" + score +
'}';
}
public static void main(String[] args) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
//獲取BeanInfo的對象
BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);
//通過BeanInfo對象獲取PropertyDescriptor屬性描述
PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
// System.out.println("通過Inspector內(nèi)省機(jī)制獲取JavaBean屬性======= 打印所有信息 ====================");
// Arrays.stream(propertyDescriptors).forEach(f->{
// System.out.println("====================================");
// System.out.println("屬性名:"+f.getName());
// System.out.println("類型:"+f.getPropertyType());
// System.out.println("get方法:"+f.getReadMethod());
// System.out.println("set方法:"+f.getWriteMethod());
// });
//
//
//
// System.out.println("通過Inspector內(nèi)省機(jī)制獲取JavaBean屬性======= 打印所有信息 ====================");
//
// for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
// System.out.println("名字:" + propertyDescriptor.getName());
// System.out.println("類型:" + propertyDescriptor.getPropertyType());
// System.out.println("get方法:" + propertyDescriptor.getReadMethod());
// System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
// }
//創(chuàng)建Employee的對象
Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
Object employee = clazz.newInstance();
//遍歷屬性描述對象
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
//打印屬性名稱
System.out.println(propertyDescriptor.getName());
//判斷屬性名稱是不是name
if (propertyDescriptor.getName().equals("name")) {
//setter方法
Method writeMethod = propertyDescriptor.getWriteMethod();
//調(diào)用setName方法
writeMethod.invoke(employee, "jack");
//getter方法
Method readMethod = propertyDescriptor.getReadMethod();
//調(diào)用getName方法
Object nameValue = readMethod.invoke(employee);
System.out.println("name屬性的值為:" + nameValue);
}
//判斷屬性名稱是否為score
if (propertyDescriptor.getName().equals("score")) {
//setter方法
Method scoreWriteMethod = propertyDescriptor.getWriteMethod();
//調(diào)用setScore方法
scoreWriteMethod.invoke(employee, new Double(3000));
//getter方法
Method scoreReadMethod = propertyDescriptor.getReadMethod();
Object scoreValue = scoreReadMethod.invoke(employee);
System.out.println("score屬性的值為:" + scoreValue);
}
}
System.out.println("當(dāng)前對象的信息:"+employee.toString());
}
}
到此這篇關(guān)于淺談Java內(nèi)省機(jī)制的文章就介紹到這了,更多相關(guān)Java內(nèi)省機(jī)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC 數(shù)據(jù)校驗(yàn)方法(必看篇)
下面小編就為大家?guī)硪黄猄pringMVC 數(shù)據(jù)校驗(yàn)方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
java jdbc連接mysql數(shù)據(jù)庫實(shí)現(xiàn)增刪改查操作
這篇文章主要為大家詳細(xì)介紹了java jdbc連接mysql數(shù)據(jù)庫實(shí)現(xiàn)增刪改查操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
IntelliJ IDEA之配置JDK的4種方式(小結(jié))
這篇文章主要介紹了IntelliJ IDEA之配置JDK的4種方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java將字節(jié)轉(zhuǎn)換為十六進(jìn)制代碼分享
我們知道,在java中,一個(gè)byte 就是一個(gè)字節(jié),也就是八個(gè)二進(jìn)制位;而4個(gè)二進(jìn)制位就可以表示一個(gè)十六進(jìn)制位,所以一個(gè)byte可以轉(zhuǎn)化為2個(gè)十六進(jìn)制位。下面我們就來詳細(xì)看下具體方法吧。2016-01-01
Java實(shí)現(xiàn)根據(jù)sql動態(tài)查詢并下載數(shù)據(jù)到excel
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)根據(jù)sql動態(tài)查詢并下載數(shù)據(jù)到excel的功能,文中的示例代碼講解詳細(xì),有需要的可以參考下2024-04-04
Java搭建簡單Netty開發(fā)環(huán)境入門教程
這篇文章主要介紹了Java搭建簡單Netty開發(fā)環(huán)境入門教程,有詳細(xì)的代碼展示和maven依賴,能夠幫助你快速上手Netty開發(fā)框架,需要的朋友可以參考下2021-06-06
java8實(shí)現(xiàn)List中對象屬性的去重方法
這篇文章主要介紹了java8實(shí)現(xiàn)List中對象屬性的去重方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
靈活控制任務(wù)執(zhí)行時(shí)間的Cron表達(dá)式范例
這篇文章主要為大家介紹了靈活控制任務(wù)執(zhí)行時(shí)間的Cron表達(dá)式范例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

