java笔记(三)——内省技术

一、用内省技术反省javaBean

(1)内省(Introspector)— JavaBean

Introspector 类为通过工具学习有关受目标 Java Bean 支持的属性、事件和方法的知识提供了一个标准方法。

(2)什么是JavaBean和属性的读写方法?

有get或set方法就是一个属性,另外所有类继承了Object类的getClass()方法,所以还有一个属性class。

(3)访问JavaBean属性的两种方式

1.直接调用bean的setXXX或getXXX方法。

2.通过内省技术访问(java.beans包提供了内省的API),内省技术访问也提供了两种方式。

       A.通过PropertyDescriptor类操作Bean的属性

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

(4)什么情况下用内省?

public class Demo1 {
	//得到bean所有属性
	@Test
	public void test1() throws IntrospectionException{
		BeanInfo info=Introspector.getBeanInfo(Person.class);
		//去掉Object里的属性 (父类的属性)
		BeanInfo info2=Introspector.getBeanInfo(Person.class,Object.class);
		PropertyDescriptor[] pds=info.getPropertyDescriptors();
		for(PropertyDescriptor pd:pds){
			System.out.println(pd.getName());
			//ab age class name	password
		}
	}
	//操纵bean的指定属性:age
	@Test
	public void test2() throws  Exception{
		Person p=new Person();
		PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);
		//得到属性的写方法(set方法),为属性赋值
		Method method=pd.getWriteMethod();
		method.invoke(p, 45);
		System.out.println(p.getAge());//45
		
		//获取属性的值(get方法)
		method=pd.getReadMethod();
		System.out.println(method.invoke(p, null));//45
	}
	//高级内容,获取当前操作的属性的类型
	@Test
	public void test3() throws  Exception{
		Person p=new Person();
		PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);
			
		//得到属性的写方法,为属性赋值
		Method method=pd.getWriteMethod();
		System.out.println(pd.getPropertyType());//int
		method.invoke(p, 45);
		System.out.println(p.getAge());//45
		
		//获取属性的值
		method=pd.getReadMethod();
		System.out.println(method.invoke(p, null));//45
	}
}

二、内省—beanutils工具包

(1)Apache组织开发了一套用于操作JavaBeanAPI,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写。

在工程下新建lib目录,导入commons-beanutils-1.8.3.jar 和支持包commons-logging-1.1.1.jar

选中两个包,右键build path/addto build path

(2)Beanutils工具包的常用类:

BeanUtils

PropertyUtils

ConvertUtils.regsiter(Converter convert, Class clazz) 自定义转换器

//使用beanUtils操纵bean的属性 ( 第三方)
public class Demo1 {
	@Test
	public void test1() throws Exception{
		Person p=new Person();
		BeanUtils.setProperty(p, "age", 456);
		System.out.println(p.getAge());//456
	}
	@Test
	public void test2() throws Exception{
		String name="aaaa";
		String age="123";
		String password="pw";
				
		Person p=new Person();
		//支持8种基本类型自动转换
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "age", age);
		BeanUtils.setProperty(p, "password", password);
		
		System.out.println(p.getName());//aaaa
		System.out.println(p.getAge());//123
		System.out.println(p.getPassword());//pw

	}
	@Test
	public void test3() throws Exception{

		String birthday="1983-12-1";
		
		//为了让日期赋值到bean的birthday属性上,给beanUtils注册一个日期转换器
		//ConvertUtils.register(converter, clazz);
		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",Locale.US);
				try {
					return df.parse(str);
				} catch (ParseException e) {
					throw new RuntimeException(e);					
				}
			}
		}, Date.class);
		Person p=new Person();
		BeanUtils.setProperty(p, "birthday", birthday);
		System.out.println(p.getBirthday());//pw
		System.out.println("___"+BeanUtils.getProperty(p, "birthday"));
	}
	public void test5() throws Exception {
		Map map=new HashMap();
		map.put("name", "aaa");
		map.put("password", "123");
		map.put("brithday", "1980-09-09");
		ConvertUtils.register(new DateLocaleConverter(), Date.class);
		Person p=new Person();
		//用map集合填充bean属性,map关键字和bean属性要一致
		BeanUtils.populate(p, map);
	}
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值