内省就是自查的意思,本质就是反射,利用反射自省类中的属性方法
内省的实现方式:
方式一:
Jdk(jre)中自带的一套自省的类库,API方法
侧重属性和属性的值,以及属性所对应的getter和setter方法
方式二:(推荐使用)
Apache的基金会提供的一套公共的自省类库Commons-BeanUtils.jar
方式一:
Jdk(jre)中自带的一套自省的类库,API方法
侧重属性和属性的值,以及属性所对应的getter和setter方法
/**
* 演示获取BeanInfo对象
* @throws Exception
*/
@Test
public void testMethod1()throws Exception{
Class clazz=User.class;
//通过自省类获取类中的所有的信息(底层还是反射)
//把反射出来的信息封装BeanInfo
BeanInfo bi=Introspector.getBeanInfo(clazz);
//原来是用反射的api操作类中的信息
//用自省的BeanInfo中的api来操作类中的信息
}
/**
* 内省指定的bean类中的属性信息
* 然后根据属性信息,获取属性的getter方法和setter方法
* @throws Exception
*/
@Test
public void testMethod2()throws Exception{
User user=new User();
//获取bean类中的的信息
BeanInfo bi=Introspector.getBeanInfo(user.getClass());
//解析BeanInfo类的对象,能解析出属性和方法
PropertyDescriptor[] pds=bi.getPropertyDescriptors();
//遍历所有的属性描述器,每一个属性描述器代表一个属性Field
for(PropertyDescriptor pd : pds){
//获取指定属性的setter方法
Method writemethod=pd.getWriteMethod();
if(writemethod!=null){
writemethod.invoke(user, "张三");
}
Method readmethod=pd.getReadMethod();
if(readmethod!=null){
Object returnValue=readmethod.invoke(user);
System.out.println(returnValue);
}
}
}
@Test
public void testMethod3() throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
User user=new User();
//user.setUserName("aaa");
PropertyDescriptor pd=new PropertyDescriptor("userName",User.class);
PropertyDescriptor pd1=new PropertyDescriptor("userPassword",User.class);
pd.setValue("userName", "张三");
pd.setValue("userName", "李四");
pd1.setValue("userPassword", "zs");
Method method1=pd.getWriteMethod();
//method1.invoke(user, "aaa");
Method method=pd.getReadMethod();
//Object obj=method.invoke(user);
String propertyname=pd.getName();
Object value=pd.getValue("userName");
System.out.println(propertyname+" "+value );
System.out.println(pd1.getValue("userPassword"));
System.out.println(pd1.getValue("userName"));
}
方式二:(超级好用)
Apache的基金会提供的一套公共的自省类库Commons-BeanUtils.jar
/**
* commons-BeanUtils工具
* 此工具中包含若干工具
*
* - MethdUtils工具类
* - ConstructorUtils工具类
* - PropertyUtils工具类
* 等...
*
* @author ZhangYang
*
*/
public class TestBeanUtilsClass {
/**
* 演示MethodUtils工具类
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
@Test
public void testMethod1() throws Exception{
User user=new User();
//用MethodUtils的api查找方法
Method method=MethodUtils.getAccessibleMethod(User.class,"setUserName",String.class);
//找到方法就可以调用方法
if(method!=null){
Object returnValue=method.invoke(user, "zhangsan");
}
//查找到了就直接调用
Object returnValue=MethodUtils.invokeMethod(user, "setUserName", "张三");
Object value=MethodUtils.invokeMethod(user, "getUserName",null);
System.out.println(value);
}
/**
* 演示ConstructorsUtils工具类
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws NoSuchMethodException
*/
@Test
public void testMethod2() throws Exception{
//用ConstructorUtils方式查找构造函数
Constructor<User> constructor=
ConstructorUtils.getAccessibleConstructor(
User.class,
new Class[]{String.class,String.class});
User user=constructor.newInstance("张三","zs");
//用ConstructorUtils方式查找构造函数后直接实例化对象
User user1=ConstructorUtils.invokeConstructor(User.class, new Object[]{"王五","ww"});
System.out.println(user.getUserName()+" "+user.getUserPassword());
System.out.println(user1.getUserName()+" "+user1.getUserPassword());
}
/**
* 演示PropertyUtils工具类
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@Test
public void testMethod3() throws Exception{
User user1=new User("zhangsan","zs");
User user2=new User();
PropertyUtils.copyProperties(user2, user1);
System.out.println(user2.getUserName()+" "+user2.getUserPassword());
PropertyUtils.setProperty(user2, "userName", "哈哈");
System.out.println(user2.getUserName()+" "+user2.getUserPassword());
}
}
User类
public class User {
private String userName;
private String userPassword;
String userAddress;
protected int age;
public String desc;
public User(){}
public User(String userName, String userPassword, String userAddress, int age, String desc) {
this.userName = userName;
this.userPassword = userPassword;
this.userAddress = userAddress;
this.age = age;
this.desc = desc;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
private float xxx(int a,float b){
return 0f;
}
protected void yyy(String a,double b){
}
String zzz(String a,Date date){
return null;
}
}