内省是基于反射实现的,主要用来操做JavaBean,经过内省能够很方便的动态得到bean的set/get方法,属性,方法名,他至关因而反射的工具类同样java
反射其实简单来讲就是经过类的名字得到对于这个类的描述,这种描述包括方法、构造器、属性的描述。举个例子来讲就是经过类名能够进行实例化对象、对类中的方法的调用、对类中属性的赋值。在许多的框架中反射是常常被应用到的技术,下面举个利用反射进行对象A的属性赋值到对象B。api
详细的反射详解,能够看不学无数——初识反射框架
其中A属性以下ide
public class A { private String a; private String b; ---------get.set方法 }
其中B属性以下,B是继承A的。因此B中也有A种a、b两个变量工具
public class B extends A{ private String c; private String d; ---------get.set方法 }
将A中a、b属性的值赋值给B中的a、b两个属性ui
public class IntrospectorAndReflect { public static void main(String[] args) throws Exception { Class classA = Class.forName("Practice.Day05.A"); -- 得到A的Class对象 Class classB = Class.forName("Practice.Day05.B"); -- 得到B的Class对象 A a = (A) classA.newInstance(); -- 实例化A对象 B b = (B) classB.newInstance(); -- 实例化B对象 a.setA("a"); a.setB("b"); fatherToChild(a,b); System.out.println(b.getA()); } public static <T>void fatherToChild(T father,T child) throws Exception { if (child.getClass().getSuperclass()!=father.getClass()){ throw new Exception("child 不是 father 的子类"); } Class<?> fatherClass = father.getClass(); --经过反射得到Class对象 Field[] declaredFields = fatherClass.getDeclaredFields(); --得到此Class对象的属性信息 for (int i = 0; i < declaredFields.length; i++) { Field field=declaredFields[i]; //得到属性的get方法 Method method=fatherClass.getDeclaredMethod("get"+upperHeadChar(field.getName())); Object obj = method.invoke(father); field.setAccessible(true);--解除方法的私有限定 field.set(child,obj);--执行set方法 } } /** * 首字母大写,in:deleteDate,out:DeleteDate */ public static String upperHeadChar(String in) { String head = in.substring(0, 1); String out = head.toUpperCase() + in.substring(1, in.length()); return out; } }
上面演示了如何经过类的全路径名得到类的Class对象,而且将Class对象进行实例化类的过程。其实这就是反射。其实有时候咱们会想,直接new一个对象这么简单的事情,何须要用反射这么麻烦呢?由于反射最大的应用就是动态加载。举个简单的例子以下,如今有A、B两个类,根据运行的须要进行加载不一样的类code
if (条件1) 加载A类 if (条件2) 加载B类
运行时肯定类型,绑定对象。动态编译最大限度地发挥了Java的灵活性,体现了多态的应用,能够减低类之间的耦合性。orm
内省是什么?咱们能够看关于java api文档中的介绍对象
The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.继承
For each of those three kinds of information, the Introspector will separately analyze the bean's class and superclasses looking for either explicit or implicit information and use that information to build a BeanInfo object that comprehensively describes the target bean.
简单理解就是内省是对于java Bean
的缺省处理,既好比在一个实体类中,有nama、address属性,那么系统会默认在此类中会有get/set方法进行得到和设置这两个值的方法。而且经过上面的介绍能够得知,内省是经过BeanInfo
类进行操做类中的属性和方法的。实例以下:
public class IntrospectorDemo { public static void main(String[] args) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(A.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors(); BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); for (PropertyDescriptor x:propertyDescriptors){ System.out.println(x.getName()); System.out.println(x.getReadMethod()); } System.out.println("-----------------------"); for (MethodDescriptor y:methodDescriptors){ System.out.println(y.getName()); } } }
能够想成内省是对于反射的更一层的封装,让咱们更加容易的获得类的信息而且操做它
在内省中经常使用的类有四个
Introspector
:将JavaBean中
的属性封装起来进行操做。在程序把一个类当作JavaBean
来看,就是调用Introspector.getBeanInfo()
方法,获得的BeanInfo
对象封装了把这个类当作JavaBean
看的结果信息,即属性的信息BeanInfo
:将类中的信息封装到BeanInfo
类中,得到了BeanInfo
对象就至关于得到了类中的全部属性信息。调用getPropertyDescriptors()
方法得到属性描述器,即得到了全部的属性信息。调用PropertyDescriptor
:PropertyDescriptor
实例封装了每一个属性特有的一些性质,好比调用getReadMethod()
方法就能得到这个属性的get方法Method,调用getWriteMethod()
方法就能得到这个属性的set方法Method。