什么是Java内省:内省是Java语言对Bean类属性、事件的一种缺省处理方法。
Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦;所以sun公司开发一套API专门来用来操作JavaBean
Person.java
package com.sunxiang.inneraware;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
TestDemo.java
package com.sunxiang.inneraware;
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 org.junit.Test;
public class TestDemo {
@Test
public void test() throws ClassNotFoundException, IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> c1 = Class.forName("com.sunxiang.inneraware.Person");
BeanInfo beanInfo = Introspector.getBeanInfo(c1,Object.class);
PropertyDescriptor[] pro = beanInfo.getPropertyDescriptors();
Person person = new Person();
System.out.println("Person的属性有:");
for(PropertyDescriptor pr:pro) {
Method writeme=pr.getWriteMethod();
if(pr.getName().equals("name")) {
writeme.invoke(person, "xiang");
}
if(pr.getName().equals("age")) {
writeme.invoke(person, 23);
}
Method method = pr.getReadMethod();
System.out.println(method.invoke(person) + " ");
}
}
@Test
public void test2() throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
PropertyDescriptor pro = new PropertyDescriptor("name", Person.class);
Person person = new Person();
Method method = pro.getWriteMethod();
method.invoke(person, "xiang");
System.out.println(pro.getReadMethod().invoke(person));
}
}
Introspector、BeanInfo的用法,不知与Class有何区别:
在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件:
BeanInfo info = Introspector.getBeanInfo( connections.getClass() );
PropertyDescriptor[] descritors = info.getPropertyDescriptors();//获得 beans PropertyDescriptor。
int size = descritors.length;
for (int index = 0 ; index < size ; index++) {
String propertyName = descritors[index].getName();
if ( connectionProviderInjectionData.containsKey( propertyName ) ) {
Method method = descritors[index].getWriteMethod();//获得应该用于写入属性值的方法。
method.invoke( connections, new Object[] {
connectionProviderInjectionData.get( propertyName ) } );
}
}
java内省与反射区别:
反射是在运行状态把Java类中的各种成分映射成相应的Java类,可以动态的获取所有的属性以及动态调用任意一个方法,强调的是运行状态。
内省(IntroSpector)是Java 语言对 JavaBean(简称VO)类属性、事件的一种缺省处理方法。内省机制是通过反射来实现。例如类User中有属性name,那么必定有getName,setName方法,我们可以通过他们来获取或者设置值。Java提供了一套API来访问某个属性的getter/setter方法,这些API存放在java.beans中。
内省涉及的类:
Introspector
BeanInfo
PropertyDescriptor
实现内省的步骤
1、通过类 Introspector 的 getBeanInfo方法 来获取某个对象的 BeanInfo 信息。
2、通过 BeanInfo 来获取属性描述器(PropertyDescriptor)。
3、通过获取的属性描述器就可以获取某个属性对应的 getter/setter 方法。
4、通过反射机制调用获取到的getter/setter 方法。
testDemo.java
package com.sunxiang.inneraware;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
public class testDemo{
@Test
public void testPropertyDescriptor() throws ClassNotFoundException, IntrospectionException {
Class<?> bean = Class.forName("com.sunxiang.inneraware.Person");
BeanInfo beanInfo = Introspector.getBeanInfo(bean, Object.class);
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor property:properties) {
Class propertyType = property.getPropertyType();
if(propertyType == null) {
continue;
}
System.out.println("Property type:" + propertyType.getName());
Method readMethod = property.getReadMethod();
if(readMethod!=null) {
System.out.println("Read method:" + readMethod);
}
Method writeMethod = property.getWriteMethod();
if(writeMethod!=null) {
System.out.println("Write method:" + writeMethod);
}
System.out.println("======================================");
}
MethodDescriptor[] methods = beanInfo.getMethodDescriptors();
for(MethodDescriptor method:methods) {
System.out.println("Public methods:" + method.getMethod().getName());
}
}
}
Person.java:
package com.sunxiang.inneraware;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}