后端开发之内省(Introspector)

  • Post author:
  • Post category:其他


开发框架时,经常需要使用java对象的属性来封装程序的数据,每次都使用反射技术完成此类操作过于麻烦,所以sun公司开发了一套API,专门用于操作java对象的属性。

内省是用于操作java对象的属性的,那么以下问题我们必须要清楚。

问题一



什么是Java对象的属性和属性的读写方法?

问题二: 如何通过内省访问到javaBean的属性 ?

1. 通过PropertyDescriptor类操作Bean的属性和更改配置文件.

public static void testPropertyDescriptor() throws Exception{
		Person p = new Person();
		PropertyDescriptor propertyDescriptor =  new PropertyDescriptor("id",Person.class);
		//获取属性的写的方法。
		Method writeMethod = propertyDescriptor.getWriteMethod();
		Method readMethod = propertyDescriptor.getReadMethod();
		propertyDescriptor.getReadMethod();
		writeMethod.invoke(p, 12);
		System.out.println(readMethod.invoke(p, null));
	}
/*
内省--->一个变态的反射.
内省主要解决 的问题: 把对象的属性数据封装 到对象中。
 */
public class Demo2 {
	@Test
	public void getAllProperty() throws IntrospectionException{
		//Introspector 内省类
		BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
		//通过BeanInfo获取所有的属性描述其
		PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); //获取一个类中的所有属性描述器
		for(PropertyDescriptor p : descriptors){
			System.out.println(p.getReadMethod()); //get方法
		}
	}
	@Test
	public  void testProperty() throws Exception{
		Person p = new Person();
		//属性描述器
		PropertyDescriptor descriptor = new PropertyDescriptor("id", Person.class);
		//获取属性对应的get或者是set方法设置或者获取属性了。
		Method  m = descriptor.getWriteMethod();  //获取属性的set方法。
		//执行该方法设置属性值
		m.invoke(p,110);
		Method readMethod = descriptor.getReadMethod(); //是获取属性的get方法
		System.out.println(readMethod.invoke(p, null));
	}
}

2. 通过Introspector类获得Bean对象的 BeanInfo,然后通过 BeanInfo 来获取属性的描述器( PropertyDescriptor ),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后通过反射机制来调用这些方法。

public static void testIntrospector() throws Exception{
		BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
		PropertyDescriptor[]  descriptor = beanInfo.getPropertyDescriptors();
		for(PropertyDescriptor itemProperty : descriptor){
			System.out.println(itemProperty.getReadMethod().getName());
		}
	}


存在的问题:

sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils。

/*
 BeanUtils:
 BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中。
 BeanUtils的好处:
 	1. BeanUtils设置属性值的时候,如果属性是基本数据 类型,BeanUtils会自动帮我转换数据类型。
  	2. BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。
  	3. BeanUtils设置属性值,如果设置的属性是其他的引用 类型数据,那么这时候必须要注册一个类型转换器。
 BeanUtilss使用的步骤:
 	1. 导包commons-logging.jar 、 commons-beanutils-1.8.0.jar
 */
public class Demo3 {

	public static void main(String[] args) throws Exception {
	//从文件中读取到的数据都是字符串的数据,或者是表单提交的数据获取到的时候也是字符串的数据。
		String id ="110";
		String name="陈其";
		String salary = "1000.0";
		String birthday = "2013-12-10";
		//注册一个类型转换器
		ConvertUtils.register(new Converter() {

			@Override
			public Object convert(Class type, Object value) { // type : 目前所遇到的数据类型。  value :目前参数的值。
				Date date = null;
				try{
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
					date = dateFormat.parse((String)value);
				}catch(Exception e){
					e.printStackTrace();
				}
				return date;
			}
		}, Date.class);
		Emp  e = new Emp();
		BeanUtils.setProperty(e, "id", id);
		BeanUtils.setProperty(e, "name",name);
		BeanUtils.setProperty(e, "salary",salary);
		BeanUtils.setProperty(e, "birthday",birthday);
		System.out.println(e);
	}
}



版权声明:本文为world_kun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。