Java 内省机制
概念
JavaBean
在实际编程中,我们常常需要一些用来包装值对象的类,例如Student、 Employee、Order,这些 类中往往没有业务方法,只是为了把需要处理的实体对象进行封装,有这样的特征:
-
属性都是私有的;
-
有无参的public构造方法;
-
对私有属性根据需要提供公有的getXxx方法以及setXxx方法;
比如:属性名称为name,则有getName方法返回属性name值, setName方法设置name值;注意方法的名称通常是get或 set加上属性名称,并把属性名称的首字母大写;这些方法称为getters/setters;getters必须有返回值没有方法参数; setter值没有返回值,有方法参数;
例如下面的例子:
符合这些特征的类,被称为JavaBean;
内省
内省(Inspector)机制就是基于反射的基础, Java语言对Bean类属性、事件的一种缺省处理方法。
只要类中有getXXX方法,或者setXXX方法,或者同时有getXXX及setXXX方法,其中getXXX方法没有方法参数,有返回值; setXXX方法没有返回值,有一个方法参数;那么内省机制就认为 XXX为一个属性;
例如下面代码
Employee类中根本没有声明age属性,仅仅是声明了这样的getter和setter.内省机制就认为age是属性
package com.shixun.introspector;public class Employee {private String name;private Double score;// age将被内省认为是属性public int getAge(){return 30;}// name将被内省认为是属性public String getName() {return name;}public void setName(String name) {this.name = name;}// score将被内省认为是属性public Double getScore() {return score;}public void setScore(Double score) {this.score = score;}public static void main(String[] args) {}
}
相关API
与Java内省有关的主要类及接口有:
-
java.beans.Introspector类
: 为获得JavaBean属性、事件、方法提供了标准方法;通常使用其中的getBeanInfo方法返回BeanInfo对象; -
Java.beans.BeanInfo接口
:不能直接实例化,通常通过Introspector类返回该类型对象,提供了返回属性描述符对象(PropertyDescriptor)、方法描述符对象(MethodDescriptor) 、 bean描述符(BeanDescriptor)对象的方法; -
Java.beans.PropertyDescriptor类
:用来描述一个属性,该属性有getter及setter方法;
可以使用PropertyDescriptor类的方法获取属性相关的信息,例如getName方法返回属性的名字:PropertyDescriptor类中定义了方法可以获取该属性的getter和setter方法
方法 方法描述 Method getReadMethod() 回属性对应的getter方法对象; Method getWriteMethod() 回属性对应的setter方法对象;
下面我们来用代码深入探究一下:
代码案例:获取属性相关信息
Employee如上面代码所示,继续编写主函数进行测试
首先用BeanInfo接口获取BeanInfo的对象,再通过BeanInfo对象获取PropertyDescriptor属性描述
//获取BeanInfo的对象BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);//通过BeanInfo对象获取PropertyDescriptor属性描述PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");Arrays.stream(propertyDescriptors).forEach(f->{System.out.println("====================================");System.out.println("属性名:"+f.getName());System.out.println("类型:"+f.getPropertyType());System.out.println("get方法:"+f.getReadMethod());System.out.println("set方法:"+f.getWriteMethod());});// 或者用增强for
System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {System.out.println("====================================");System.out.println("名字:" + propertyDescriptor.getName());System.out.println("类型:" + propertyDescriptor.getPropertyType());System.out.println("get方法:" + propertyDescriptor.getReadMethod());System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
}
运行结果如下:
我们也可以通过反射调用这里获取的get或set方法
//创建Employee的对象
Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
Object employee = clazz.newInstance();//遍历属性描述对象
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {//打印属性名称System.out.println(propertyDescriptor.getName());//判断属性名称是不是nameif (propertyDescriptor.getName().equals("name")) {//setter方法Method writeMethod = propertyDescriptor.getWriteMethod();//调用setName方法writeMethod.invoke(employee, "jack");//getter方法Method readMethod = propertyDescriptor.getReadMethod();//调用getName方法Object nameValue = readMethod.invoke(employee);System.out.println("name属性的值为:" + nameValue);}//判断属性名称是否为scoreif (propertyDescriptor.getName().equals("score")) {//setter方法Method scoreWriteMethod = propertyDescriptor.getWriteMethod();//调用setScore方法scoreWriteMethod.invoke(employee, new Double(3000));//getter方法Method scoreReadMethod = propertyDescriptor.getReadMethod();Object scoreValue = scoreReadMethod.invoke(employee);System.out.println("score属性的值为:" + scoreValue);}
}
System.out.println("当前对象的信息:"+employee.toString());
运行结果如下所示:
全部代码附在最下方!!!!!!
内省属性的注意事项
-
很多框架都使用了内省机制检索对象的属性,定义属性名字时,名字最好起码以两个小写字母开
头,例如stuName,而不要使用sName,某些情况下,可能会导致检索属性失败; -
内省机制检索属性时,是根据getter和setter方法确认属性名字,而不是根据类里声明
的成员变量名称决定;
完整代码
package com.shixun.introspector;import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;public class Employee {private String name;private Double score;// age将被内省认为是属性public int getAge() {return 30;}// name将被内省认为是属性public String getName() {return name;}public void setName(String name) {this.name = name;}// score将被内省认为是属性public Double getScore() {return score;}public void setScore(Double score) {this.score = score;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", score=" + score +'}';}public static void main(String[] args) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {//获取BeanInfo的对象BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);//通过BeanInfo对象获取PropertyDescriptor属性描述PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
// System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
// Arrays.stream(propertyDescriptors).forEach(f->{
// System.out.println("====================================");
// System.out.println("属性名:"+f.getName());
// System.out.println("类型:"+f.getPropertyType());
// System.out.println("get方法:"+f.getReadMethod());
// System.out.println("set方法:"+f.getWriteMethod());
// });
//
//
//
// System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
//
// for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
// System.out.println("名字:" + propertyDescriptor.getName());
// System.out.println("类型:" + propertyDescriptor.getPropertyType());
// System.out.println("get方法:" + propertyDescriptor.getReadMethod());
// System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
// }//创建Employee的对象Class<?> clazz = Class.forName("com.shixun.introspector.Employee");Object employee = clazz.newInstance();//遍历属性描述对象for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {//打印属性名称System.out.println(propertyDescriptor.getName());//判断属性名称是不是nameif (propertyDescriptor.getName().equals("name")) {//setter方法Method writeMethod = propertyDescriptor.getWriteMethod();//调用setName方法writeMethod.invoke(employee, "jack");//getter方法Method readMethod = propertyDescriptor.getReadMethod();//调用getName方法Object nameValue = readMethod.invoke(employee);System.out.println("name属性的值为:" + nameValue);}//判断属性名称是否为scoreif (propertyDescriptor.getName().equals("score")) {//setter方法Method scoreWriteMethod = propertyDescriptor.getWriteMethod();//调用setScore方法scoreWriteMethod.invoke(employee, new Double(3000));//getter方法Method scoreReadMethod = propertyDescriptor.getReadMethod();Object scoreValue = scoreReadMethod.invoke(employee);System.out.println("score属性的值为:" + scoreValue);}}System.out.println("当前对象的信息:"+employee.toString());}
}
Java 内省机制
概念
JavaBean
在实际编程中,我们常常需要一些用来包装值对象的类,例如Student、 Employee、Order,这些 类中往往没有业务方法,只是为了把需要处理的实体对象进行封装,有这样的特征:
-
属性都是私有的;
-
有无参的public构造方法;
-
对私有属性根据需要提供公有的getXxx方法以及setXxx方法;
比如:属性名称为name,则有getName方法返回属性name值, setName方法设置name值;注意方法的名称通常是get或 set加上属性名称,并把属性名称的首字母大写;这些方法称为getters/setters;getters必须有返回值没有方法参数; setter值没有返回值,有方法参数;
例如下面的例子:
符合这些特征的类,被称为JavaBean;
内省
内省(Inspector)机制就是基于反射的基础, Java语言对Bean类属性、事件的一种缺省处理方法。
只要类中有getXXX方法,或者setXXX方法,或者同时有getXXX及setXXX方法,其中getXXX方法没有方法参数,有返回值; setXXX方法没有返回值,有一个方法参数;那么内省机制就认为 XXX为一个属性;
例如下面代码
Employee类中根本没有声明age属性,仅仅是声明了这样的getter和setter.内省机制就认为age是属性
package com.shixun.introspector;public class Employee {private String name;private Double score;// age将被内省认为是属性public int getAge(){return 30;}// name将被内省认为是属性public String getName() {return name;}public void setName(String name) {this.name = name;}// score将被内省认为是属性public Double getScore() {return score;}public void setScore(Double score) {this.score = score;}public static void main(String[] args) {}
}
相关API
与Java内省有关的主要类及接口有:
-
java.beans.Introspector类
: 为获得JavaBean属性、事件、方法提供了标准方法;通常使用其中的getBeanInfo方法返回BeanInfo对象; -
Java.beans.BeanInfo接口
:不能直接实例化,通常通过Introspector类返回该类型对象,提供了返回属性描述符对象(PropertyDescriptor)、方法描述符对象(MethodDescriptor) 、 bean描述符(BeanDescriptor)对象的方法; -
Java.beans.PropertyDescriptor类
:用来描述一个属性,该属性有getter及setter方法;
可以使用PropertyDescriptor类的方法获取属性相关的信息,例如getName方法返回属性的名字:PropertyDescriptor类中定义了方法可以获取该属性的getter和setter方法
方法 方法描述 Method getReadMethod() 回属性对应的getter方法对象; Method getWriteMethod() 回属性对应的setter方法对象;
下面我们来用代码深入探究一下:
代码案例:获取属性相关信息
Employee如上面代码所示,继续编写主函数进行测试
首先用BeanInfo接口获取BeanInfo的对象,再通过BeanInfo对象获取PropertyDescriptor属性描述
//获取BeanInfo的对象BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);//通过BeanInfo对象获取PropertyDescriptor属性描述PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");Arrays.stream(propertyDescriptors).forEach(f->{System.out.println("====================================");System.out.println("属性名:"+f.getName());System.out.println("类型:"+f.getPropertyType());System.out.println("get方法:"+f.getReadMethod());System.out.println("set方法:"+f.getWriteMethod());});// 或者用增强for
System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {System.out.println("====================================");System.out.println("名字:" + propertyDescriptor.getName());System.out.println("类型:" + propertyDescriptor.getPropertyType());System.out.println("get方法:" + propertyDescriptor.getReadMethod());System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
}
运行结果如下:
我们也可以通过反射调用这里获取的get或set方法
//创建Employee的对象
Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
Object employee = clazz.newInstance();//遍历属性描述对象
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {//打印属性名称System.out.println(propertyDescriptor.getName());//判断属性名称是不是nameif (propertyDescriptor.getName().equals("name")) {//setter方法Method writeMethod = propertyDescriptor.getWriteMethod();//调用setName方法writeMethod.invoke(employee, "jack");//getter方法Method readMethod = propertyDescriptor.getReadMethod();//调用getName方法Object nameValue = readMethod.invoke(employee);System.out.println("name属性的值为:" + nameValue);}//判断属性名称是否为scoreif (propertyDescriptor.getName().equals("score")) {//setter方法Method scoreWriteMethod = propertyDescriptor.getWriteMethod();//调用setScore方法scoreWriteMethod.invoke(employee, new Double(3000));//getter方法Method scoreReadMethod = propertyDescriptor.getReadMethod();Object scoreValue = scoreReadMethod.invoke(employee);System.out.println("score属性的值为:" + scoreValue);}
}
System.out.println("当前对象的信息:"+employee.toString());
运行结果如下所示:
全部代码附在最下方!!!!!!
内省属性的注意事项
-
很多框架都使用了内省机制检索对象的属性,定义属性名字时,名字最好起码以两个小写字母开
头,例如stuName,而不要使用sName,某些情况下,可能会导致检索属性失败; -
内省机制检索属性时,是根据getter和setter方法确认属性名字,而不是根据类里声明
的成员变量名称决定;
完整代码
package com.shixun.introspector;import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;public class Employee {private String name;private Double score;// age将被内省认为是属性public int getAge() {return 30;}// name将被内省认为是属性public String getName() {return name;}public void setName(String name) {this.name = name;}// score将被内省认为是属性public Double getScore() {return score;}public void setScore(Double score) {this.score = score;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", score=" + score +'}';}public static void main(String[] args) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {//获取BeanInfo的对象BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);//通过BeanInfo对象获取PropertyDescriptor属性描述PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
// System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
// Arrays.stream(propertyDescriptors).forEach(f->{
// System.out.println("====================================");
// System.out.println("属性名:"+f.getName());
// System.out.println("类型:"+f.getPropertyType());
// System.out.println("get方法:"+f.getReadMethod());
// System.out.println("set方法:"+f.getWriteMethod());
// });
//
//
//
// System.out.println("通过Inspector内省机制获取JavaBean属性======= 打印所有信息 ====================");
//
// for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
// System.out.println("名字:" + propertyDescriptor.getName());
// System.out.println("类型:" + propertyDescriptor.getPropertyType());
// System.out.println("get方法:" + propertyDescriptor.getReadMethod());
// System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
// }//创建Employee的对象Class<?> clazz = Class.forName("com.shixun.introspector.Employee");Object employee = clazz.newInstance();//遍历属性描述对象for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {//打印属性名称System.out.println(propertyDescriptor.getName());//判断属性名称是不是nameif (propertyDescriptor.getName().equals("name")) {//setter方法Method writeMethod = propertyDescriptor.getWriteMethod();//调用setName方法writeMethod.invoke(employee, "jack");//getter方法Method readMethod = propertyDescriptor.getReadMethod();//调用getName方法Object nameValue = readMethod.invoke(employee);System.out.println("name属性的值为:" + nameValue);}//判断属性名称是否为scoreif (propertyDescriptor.getName().equals("score")) {//setter方法Method scoreWriteMethod = propertyDescriptor.getWriteMethod();//调用setScore方法scoreWriteMethod.invoke(employee, new Double(3000));//getter方法Method scoreReadMethod = propertyDescriptor.getReadMethod();Object scoreValue = scoreReadMethod.invoke(employee);System.out.println("score属性的值为:" + scoreValue);}}System.out.println("当前对象的信息:"+employee.toString());}
}