Java的内省技术

什么是内省

在计算机科学中,内省是指计算机程序在运行时(Run time)检查对象(Object)类型的一种能力,通常也可以称作运行时类型检查。 不应该将内省和反射混淆。相对于内省,反射更进一步,是指计算机程序在运行时(Run time)可以访问、检测和修改它本身状态或行为的一种能力。

内省和反射的区别

反射是在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态。
.
内省(IntroSpector)是Java 语言针对 Bean 类属性、事件的一种缺省处理方法。 JavaBean是一种特殊的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。如果在两个模块之间传递信息,可以将信息封装进JavaBean中,这种对象称为“值对象”(Value Object),或“VO”。方法比较少。这些信息储存在类的私有变量中,通过set()、get()获得。内省机制是通过反射来实现的,BeanInfo用来暴露一个bean的属性、方法和事件,以后我们就可以操纵该JavaBean的属性。
在这里插入图片描述
在Java内省中,用到的基本上就是上述几个类。

通过BeanInfo这个类就可以获取到类中的方法和属性。例如类 A 中有属性 name, 那我们可以通过 getName,setName 来得到其值或者设置新的值。通过 getName/setName 来访问 name 属性,这就是默认的规则。

Java 中提供了一套 API 用来访问某个属性的 getter/setter 方法,通过这些 API 可以使你不需要了解这个规则(但你最好还是要搞清楚),这些 API 存放于包 java.beans 中,

一般的做法是通过类 Introspector 的 getBeanInfo方法 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法,这就是内省机制。

实例

自定义一个bean类:

/**
 * 
 */
package beanutils;

import java.util.Date;

/**
 * JavaBean
 *
 */
public class Person {
	private String name;// 字段
	private String password;
	private int age;
	private Date birthday;

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAb() { // ab也是bean中的一个属性!
		return null;

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

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

}

使用内省API操作bean的属性

/**
 * 
 */
package cn.itcast.introspector;

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

import org.junit.Test;

/**
 * 使用内省API操作bean的属性
 *
 */
public class Demo1 {
	// 1.得到bean的所有属性
	@Test
	public void test1() throws Exception {
		BeanInfo info = Introspector.getBeanInfo(Person.class, Object.class);// 不自省从父类继承的属性
		PropertyDescriptor[] pds = info.getPropertyDescriptors();// 取得属性描述器
		for (PropertyDescriptor pd : pds) {
			System.out.println(pd.getName());
		}
	}

	// 2.操纵bean的指定属性:age
	@Test
	public void test2() throws Exception {
		Person p = new Person();

		PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);

		// 得到属性的写方法,为属性赋值
		Method method = pd.getWriteMethod(); // setAge
		method.invoke(p, 24);

		// 获取属性的值
		method = pd.getReadMethod(); // getAge()
		System.out.println(method.invoke(p, null));
	}
	
	// 3.获取当前操作的属性的类型
		@Test
		public void test3() throws Exception {
			Person p = new Person();

			PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
			System.out.println(pd.getPropertyType());
			
		}
}

以上的操作略显繁琐,Apache组织开发了一套用于操作JavaBean的API——beanutils,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写。

beanutils工具包

BeanUtils工具包的下载地址:http://commons.apache.org/beanutils/;注意:BeanUtils的包依赖于logging包,logging包的下载地址为:http://commons.apache.org/logging/。

在工程下新建lib目录,导入commons-beanutils-1.9.3.jar 和支持包commons-logging-1.2.jar
选中两个包,右键build path/add to build path
在这里插入图片描述
Beanutils工具包的常用类和方法:

  • BeanUtils
  • PropertyUtils
  • ConvertUtils.regsiter(Converter convert, Class clazz)

对bean中的某个属性进行赋值

下面的例子test1(),直接通过BeanUtils类的setProperty方法来对bean中的某个属性进行赋值。

@Test
	public void test1() throws IllegalAccessException, InvocationTargetException {

		Person p = new Person();
		BeanUtils.setProperty(p, "name", "yaoer");

		System.out.println(p.getName());
	}

在这里插入图片描述

自定义转换器

下面的test2,演示了如何自定义一个转换器。
因为用户提交的"1994-10-12"是个字符串,而bean中的birthday是个Date类型的属性,由于这套API中,String类型自动转化仅限于8种基本类型,所以无法直接将字符串转换为Date。这就需要我们自定义一个转换器。
重点:ConvertUtils.regsiter(Converter convert, Class clazz)方法。

@Test

	public void test2() throws IllegalAccessException, InvocationTargetException {
		Person p = new Person();
		// 模拟用户提交的表单
		String name = "yaoer";
		String password = "123";
		String age = "24";
		String birthday = "1994-10-12";

		// 给beanUtils注册一个日期转换器
		ConvertUtils.register(new Converter() {

			public Object convert(Class type, Object value) {

				if (value == null) {
					return null;
				}
				if (!(value instanceof String)) {
					throw new ConversionException("只支持String类型的转换哦!");

				}
				String str = (String) value;
				if (str.trim().equals("")) {
					return null;
				}

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				try {
					return df.parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e); // 异常链不能断
				}

			}
		}, Date.class);

		// 封装到p对象中

		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age); // 自动将数据转换(基本类型)
		BeanUtils.setProperty(p, "birthday", birthday);

		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
		System.out.println(p.getBirthday());

	}

在这里插入图片描述
如图,成功转换。
需要注意的一点是Date的导包,import java.util.Date是正确的,而不是sql的,否则会报错。

import java.util.Date;

使用API中自带的转换器:DateLocaleConverter

@Test
	public void test3() throws  ConversionException, IllegalAccessException, InvocationTargetException{
		Person p = new Person();
		// 模拟用户提交的表单
		String name = "yaoer";
		String password = "123";
		String age = "24";
		String birthday = "1994-10-12";

		ConvertUtils.register(new DateLocaleConverter(), Date.class);

		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age); // 自动将数据转换(基本类型)
		BeanUtils.setProperty(p, "birthday", birthday);

		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
		Date date = p.getBirthday();
		System.out.println(date.toString());

	}

本例运行时报错,暂时还未找到原因。
在这里插入图片描述

用map集合中的值填充bean的属性

@Test
	public void test4() throws IllegalAccessException, InvocationTargetException {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		map.put("name","zhuzhu");
		map.put("password","123");
		map.put("age","24");
		map.put("birthday","1994-10-12");
		
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		Person bean = new Person();
		BeanUtils.populate(bean, map);//用map集合中的值填充bean的属性
		
		System.out.println(bean.getName());
		System.out.println(bean.getPassword());
		System.out.println(bean.getAge());
		System.out.println(bean.getBirthday());
		
	}

由于同样使用了DateLocaleConverter,所以和上例报了同样的错误。
如果将转换器替换为test2例子中自定义的转换器,那么可以成功通过测试:
在这里插入图片描述

总结

内省是基于反射实现的,主要用来操作JavaBean,通过内省可以很方便的动态获得bean的set/get方法,属性,方法名。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值