黑马程序员-内省学习笔记

      内省(Introspector) :是Java 语言对JavaBean类属性、事件的一种缺省处理方法。JavaBean:是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。如果在两个模块之间传递信息,可以将信息封装进JavaBean中,这种对象称为“值对象”(Value Object),或“VO”。这些信息储存在类的私有变量中,通过setXxx()方法设置值、getXxx()方法获取值。

package it.shuangxi.instrospector;
/**
 * 一个JavaBean
 */
public class StudentInfo {
	private long userId;
	private String userName;
	private int age;
	private String emailAddress;

	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	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;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
}

在上面的类(StudentInfo)中有属性userName,并提供了getUserName(), setUserName()方法,我们可以通过getUserName/setUserName来访问userName属性,这就是默认的规则。Java JDK中提供了一套API用来访问某个属性的getter/setter方法,这就是内省。

--------------------------------------PropertyDescriptor类--------------------------------------

PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。主要方法:

1. getPropertyType(),获得属性的Class对象;

2. getReadMethod(),获得用于读取属性值的方法;

3. getWriteMethod(),获得用于写入属性值的方法;

4. hashCode(),获取对象的哈希值;

5. setReadMethod(Method readMethod),设置用于读取属性值的方法;

6. setWriteMethod(Method writeMethod),设置用于写入属性值的方法。

--------------------------------------Introspector类--------------------------------------

       将JavaBean中的属性封装起来进行操作。在程序把一个类当做JavaBean,调用Introspector.getBeanInfo()方法,就得到的BeanInfo对象封装了,把这个类当做JavaBean的属性的信息。getPropertyDescriptors(),获得属性的描述,可以采用遍历BeanInfo的方法,来查找、设置类的属性。

利用反射方法,通过属性名设定或获取对应的值:

 

package it.shuangxi.instrospector;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

public class BeanInfoUtil {
	
	/************************PropertyDescriptor类*********************************/
	// 设置bean的某个属性值
	public static void setProperty(StudentInfo userInfo, String userName)throws Exception {
		// 获取bean的某个属性的描述符
		PropertyDescriptor propDesc = new PropertyDescriptor(userName,StudentInfo.class);
		// 获得用于写入属性值的方法
		Method methodSetUserName = propDesc.getWriteMethod();
		// 写入属性值
		methodSetUserName.invoke(userInfo, "setProperty");
		System.out.println("setProperty set userName:" + userInfo.getUserName());
	}

	// 获取bean的某个属性值
	public static void getProperty(StudentInfo userInfo, String userName)throws Exception {
		// 获取Bean的某个属性的描述符
		PropertyDescriptor proDescriptor = new PropertyDescriptor(userName,StudentInfo.class);
		// 获得用于读取属性值的方法
		Method methodGetUserName = proDescriptor.getReadMethod();
		// 读取属性值
		Object objUserName = methodGetUserName.invoke(userInfo);
		System.out.println("setProperty get userName:" + objUserName.toString());
	}

	/************************Introspector类*********************************/
	// 通过内省设置bean的某个属性值
	public static void setPropertyByIntrospector(StudentInfo userInfo,	String userName) throws Exception {
		// 获取bean信息
		BeanInfo beanInfo = Introspector.getBeanInfo(StudentInfo.class);
		// 获取bean的所有属性列表
		PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();
		// 遍历属性列表,查找指定的属性
		if (proDescrtptors != null && proDescrtptors.length > 0) {
			for (PropertyDescriptor propDesc : proDescrtptors) {
				// 找到则写入属性值
				if (propDesc.getName().equals(userName)) {
					//获取该属性的写方法
					Method methodSetUserName = propDesc.getWriteMethod();
					// 写入属性值
					methodSetUserName.invoke(userInfo, "setPropertyByIntrospector"); 
					System.out.println("setPropertyByIntrospector set userName:" + userInfo.getUserName());
					//写完后就不用在往下循环了
					break;
				}
			}
		}
	}

	// 通过内省获取bean的某个属性值
	public static void getPropertyByIntrospector(StudentInfo userInfo,String userName) throws Exception {
		// 获取bean信息
		BeanInfo beanInfo = Introspector.getBeanInfo(StudentInfo.class);
		// 获取bean的所有属性列表
		PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();
		if (proDescrtptors != null && proDescrtptors.length > 0) {
			//遍历属性,找到要读取到字段
			for (PropertyDescriptor propDesc : proDescrtptors) {
				//找到字段就读取
				if (propDesc.getName().equals(userName)) {
					//通过反射获取读方法
					Method methodGetUserName = propDesc.getReadMethod();
					//用反射调用方法,并接受返回值
					Object objUserName = methodGetUserName.invoke(userInfo);
					//打印出该属性值
					System.out.println("setPropertyByIntrospector get userName:" + objUserName.toString());
					//结束循环
					break;
				}
			}
		}
	}
}
package it.shuangxi.instrospector;

public class BeanInfoTest {

	/**
	 * 测试类
	 */
	public static void main(String[] args) {
		//实例话一个用户类
		StudentInfo userInfo = new StudentInfo();
		//为用户类设置姓名
		userInfo.setUserName("userInfo.setUserName");
		try {
			//获取userInfo的userName的值
			BeanInfoUtil.getProperty(userInfo, "userName");
			//设置userInfo的userName的值
			BeanInfoUtil.setProperty(userInfo, "userName");
			//获取userInfo的userName的值
			BeanInfoUtil.getProperty(userInfo, "userName");
			//设置userInfo的userName的值
			BeanInfoUtil.setPropertyByIntrospector(userInfo, "userName");
			//获取userInfo的userName的值
			BeanInfoUtil.getPropertyByIntrospector(userInfo, "userName");
			//获取userInfo的age的值
		//	BeanInfoUtil.setProperty(userInfo, "age"); // IllegalArgumentException
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

结果:

setProperty get userName:userInfo.setUserName
setProperty set userName:setProperty
setProperty get userName:setProperty
setPropertyByIntrospector set userName:setPropertyByIntrospector
setPropertyByIntrospector get userName:setPropertyByIntrospector

通过这两个类的使用比较可以看出,都是需要获得PropertyDescriptor,方式有所不同:前者通过创建对象直接获得,后者需要遍历,所以使用PropertyDescriptor类更加方便。

说明:上面代码中,BeanInfoUtil.setProperty(userInfo,"age");抛异常,报错是应为age属性是int数据类型,而setProperty方法里面默认给属性赋值是String类型。所以会爆出argument type mismatch参数类型不匹配的错误信息。

猜你喜欢

转载自shuangxi-zhu.iteye.com/blog/2247193