普通的JavaBean Student类
package com.testBean;
public class Student {
private String name;
private int age;
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final int getAge() {
return age;
}
public final void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student( ){
super();
this.name = "zhang";
this.age = 13;
}
}
内省的实现
package com.testBean;
import java.beans.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BeanTestBasic {
public static void main(String[] args) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Student s=new Student();
String propertiesName="name";
PropertyDescriptor pd=new PropertyDescriptor(propertiesName,s.getClass());
Method methodGetName=pd.getReadMethod();
Object retVal=methodGetName.invoke(s);
System.out.println(retVal);
System.out.println("---------------setMethod------------------");
PropertyDescriptor pd1=new PropertyDescriptor("name",s.getClass());
Method methodSetName=pd1.getWriteMethod();
methodSetName.invoke(s,"lisi");
System.out.println(s.getName());
}
}