复用代码系列:Spring的BeanUtils操作

package com.spring.tools;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;

public class BeanUtilsTest {
	/**
	 * 实例化
	 */
	@Test
	public void testInstantiate() {
		//通过无参数构造函数实例化
		Object obj = BeanUtils.instantiate(Person.class);
		//Object obj = BeanUtils.instantiateClass(Person.class); //与BeanUtils.instantiate(Person.class)一样
		if(obj instanceof Person) {
			System.out.println(((Person) obj).getName());
		}
		
		//通过有参数构造函数实例化
		Object obj2;
		try {
			obj2 = BeanUtils.instantiateClass(Person.class.getDeclaredConstructor(String.class), "a");
			if(obj2 instanceof Person) {
				System.out.println(((Person) obj2).getName());
			}
		} catch (BeanInstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查找方法
	 * @throws Exception
	 */
	@Test
	public void testFindDeclaredMethod() throws Exception {
		User user = new User();
		user.setCode("1");
		user.setName("suncht");
		
		//在所有方法中查找
		Method method1 = BeanUtils.findDeclaredMethod(User.class, "getCode");
		Object code1 = method1.invoke(user);
		System.out.println(code1);
		
		//在公共方法中查找
		Method method2 = BeanUtils.findMethod(User.class, "getName");
		Object code2 = method2.invoke(user);
		System.out.println(code2);
		
	}
	
	/**
	 * 查找属性
	 * @throws Exception
	 */
	@Test
	public void testPropertyDescriptor() throws Exception {
		User user = new User();
		user.setCode("222");
		user.setName("suncht");
		
		//获取属性
		PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(User.class, "code");
		
		//获取属性的get方法
		Method getMethod = pd.getReadMethod();
		Object code1 = getMethod.invoke(user);
		System.out.println(code1);
		
		//获取属性的get方法
		Method setMethod = pd.getWriteMethod();
		setMethod.invoke(user, "333");
		System.out.println(user.getCode());
		
		//根据方法查找属性
		Method getCodeMethod = BeanUtils.findDeclaredMethod(User.class, "getCode2");
		PropertyDescriptor pd2 = BeanUtils.findPropertyForMethod(getCodeMethod);
		System.out.println(pd2);
	}
	
	
	public static class Person {
		private String name = "aa";
		
		public Person() {
			
		}
		
		public Person(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
	
	public static class User extends Person {
		private String code;

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}
		
		private String getCode2() {
			return code;
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值