现在的位置: 首页 > 综合 > 正文

java-javabean Introspector的应用

2017年08月23日 ⁄ 综合 ⁄ 共 963字 ⁄ 字号 评论关闭

Introspector 类为通过工具学习有关受目标 Java Bean 支持的属性、事件和方法的知识提供了一个标准方法。
对于这三种信息,Introspector 将分别分析 bean 的类和超类,寻找显式或隐式信息,使用这些信息构建一个全面描述目标 bean 的 BeanInfo 对象。

package com.javabean;

public class UserBean {
	private String userName;
	private int age;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

package com.javabean;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;


public class IntrospectorTest {
	public static void main(String args[]){
		try {
			BeanInfo bi = Introspector.getBeanInfo(UserBean.class);
			
			 PropertyDescriptor[] pds = bi.getPropertyDescriptors();
			 for(PropertyDescriptor ps : pds){
				 System.out.println("name = "+ps.getName()+",value = "+ps.getValue(ps.getName()));
				 
			 }
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

更多用法详见java api文档

http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

抱歉!评论已关闭.