Java 内省introspector相关原理代码解析
1.JavaBean(有get/set属性,和默认构造器等规范的java类)
importjava.util.Date;
publicclassStudent{
//这是字段
privateStringname;
privateintage;
privateDatebirthday;
//这是属性
//(get、set开头的方法,getName、setName算一个属性,单独一个set或get也算一个属性)
//属性名为去掉get、set后第一个大写字母变小写字母。
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetAge(){
returnage;
}
publicvoidsetAge(intage){
this.age=age;
}
publicintgetAbc(){//注意这也是一个属性,属性名为abc
return10;
}
/*
publicintgetefg(){//注意这也是一个属性,属性名为efg
return10;
}*/
publicDategetBirthday(){
returnbirthday;
}
publicvoidsetBirthday(Datebirthday){
this.birthday=birthday;
}
}
测试
importjava.beans.BeanInfo;
importjava.beans.Introspector;
importjava.beans.PropertyDescriptor;
importjava.lang.reflect.Method;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
importorg.apache.commons.beanutils.BeanUtils;
importorg.apache.commons.beanutils.ConvertUtils;
importorg.apache.commons.beanutils.locale.converters.DateLocaleConverter;
publicclassTest1{
publicstaticvoidmain(String[]args)throwsException{
test05();
}
//获取属性描述器Introspector.getBeanInfo(Student.class).getPropertyDescriptors();
privatestaticvoidtest01()throwsException{
BeanInfobf=Introspector.getBeanInfo(Student.class);
PropertyDescriptor[]pds=bf.getPropertyDescriptors();
for(PropertyDescriptorpd:pds){
System.out.println(pd.getName());
}
/*
abc
age
class//这个是Object类里的
name
*/
}
//使用内省调用set、get方法
privatestaticvoidtest02()throwsException{
Studentstu=newStudent();
PropertyDescriptorpd=newPropertyDescriptor("name",Student.class);
Methodsetter=pd.getWriteMethod();
setter.invoke(stu,"tom");
Methodgetter=pd.getReadMethod();
System.out.println(getter.invoke(stu));
}
/**
*以上使用的java源码里的java.beans包
*接下来有更方便的,Apache组织提供的commons-beanutils-1.8.3.jar
*导入:commons-beanutils-1.8.3.jarcommons-logging-1.1.1.jar
*/
privatestaticvoidtest03()throwsException{
Studentstu=newStudent();
BeanUtils.setProperty(stu,"name","白居易");
System.out.println(stu.getName());
Stringname=BeanUtils.getProperty(stu,"name");
System.out.println(name);
//BeanUtils支持8中基本类型自动转换
BeanUtils.setProperty(stu,"age",19);
BeanUtils.setProperty(stu,"age","18");
System.out.println(stu.getAge());
//PropertyUtils.setSimpleProperty(stu,name,value);
}
privatestaticvoidtest04()throwsException{
Studentstu=newStudent();
//set/get日期Date
ConvertUtils.register(newDateLocaleConverter(),Date.class);
BeanUtils.setProperty(stu,"birthday","1999-11-10");
System.out.println(stu.getBirthday());
Strings=BeanUtils.getProperty(stu,"birthday");
System.out.println(s);
}
/**
*一下整个赋值给javaBean对象,使用BeanUtils.populate
*@throwsException
*/
privatestaticvoidtest05()throwsException{
Studentstu=newStudent();
Mapm=newHashMap();
m.put("name","Lee");//注意:key名一定要与对象中的变量名一致
m.put("age","18");//注意:key名一定要与对象中的变量名一致
m.put("birthday","2020-7-4");//注意:key名一定要与对象中的变量名一致
ConvertUtils.register(newDateLocaleConverter(),Date.class);
BeanUtils.populate(stu,m);
System.out.println(stu.getBirthday());
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。