内省

内省就是自查的意思,本质就是反射,利用反射自省类中的属性方法
内省的实现方式:
方式一:
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;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值