首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

使用 Introspector 動態提取 Bean 資訊

     編輯:Java 相關知識

核心 API

Introspector.getBeanInfo(beanClass, stopClass) 返回 BeanInfo,停止於指定父類(通常為 Object.class)。

BeanDumper 工具實作

遍歷三大描述符:

  • PropertyDescriptor:getPropertyType()、getName()、getReadMethod()/getWriteMethod()。
  • MethodDescriptor:公開方法列表(含屬性存取器)。
  • EventSetDescriptor:getListenerType()、getListenerMethods()、getAddListenerMethod()/getRemoveListenerMethod()。

輸出範例

對 Frog Bean 執行 java BeanDumper frogbean.Frog 可見:

  • 屬性:color(Color)、spots(Spots)、jumper(boolean)、jumps(int)。
  • 方法:含 croak() 等普通公開方法。
  • 事件:KeyListener、ActionListener 完整描述符。

應用場景

視覺化建構工具拖放 Bean 時,即透過內省動態構建屬性表、事件綁定 UI,無需硬編碼。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved