package cn.itcast.introspector;
import java.util.Date;
public class Student {
private String name; //字段
private int age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {//属性 name
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() { //age
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getAbc(){ //abc
return 10;
}
}
package cn.itcast.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo1 {
/*
* Introspector构建一个BeanInfo对象。
* BeanInfo将一个类的所有属性进行了封装
* //属性描述器
PropertyDescriptor[] pds = bf.getPropertyDescriptors();
PropertyDescriptor对象:
Method getReadMethod()
获得应该用于读取属性值的方法。
Method getWriteMethod()
获得应该用于写入属性值的方法。
* */
@Test
public void test1() throws Exception{
//将Student中的所有属性封装到BeanInfo对象中
BeanInfo bf = Introspector.getBeanInfo(Student.class);
//属性描述器
PropertyDescriptor[] pds = bf.getPropertyDescriptors();
// System.out.println(pds.length);
for (PropertyDescriptor pd : pds) {
System.out.println(pd.getName());
}
}
@Test
public void test2() throws Exception{
Student stu = new Student();
//得到指定的属性对象
PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
Method setter = pd.getWriteMethod();//得到setter方法:setName()
setter.invoke(stu, "tom");
Method getter = pd.getReadMethod(); //得到getter方法:getName()
System.out.println(getter.invoke(stu, null));
}
@Test
public void test3() throws Exception{
Student stu = new Student();
//得到指定的属性对象
PropertyDescriptor pd = new PropertyDescriptor("age", Student.class);
Method setter = pd.getWriteMethod();//得到setter方法:setName()
setter.invoke(stu, 18);
Method getter = pd.getReadMethod(); //得到getter方法:getName()
System.out.println(getter.invoke(stu, null));
}
}
package cn.itcast.introspector;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
public class Demo2 {
@Test
public void test1() throws Exception{
Student stu = new Student();
BeanUtils.setProperty(stu, "name", "张三"); //给属性赋值
String s = BeanUtils.getProperty(stu, "name");
System.out.println(s);
}
@Test
public void test2() throws Exception{
Student stu = new Student();
//BeanUtils默认支持8种基本数据类型,自动转换
BeanUtils.setProperty(stu, "age", "18"); //给属性赋值
String s = BeanUtils.getProperty(stu, "age");
System.out.println(s);
}
@Test
public void test3() throws Exception{
Student stu = new Student();
//注册类型转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(stu, "birthday", "1990-11-12"); //给属性赋值
String s = BeanUtils.getProperty(stu, "birthday");
System.out.println(s);
}
@Test
public void test4() throws Exception{
Student stu = new Student();
//注册类型转换器
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
if(value instanceof String){
String v = (String) value;
try {
return sdf.parse(v);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
return null;
}
}, Date.class);
BeanUtils.setProperty(stu, "birthday", "1995-11-12"); //给属性赋值
String s = BeanUtils.getProperty(stu, "birthday");
System.out.println(s);
}
@Test
public void test5() throws Exception{
//
Map m = new HashMap();
m.put("name", "张三"); //key名一定要与对象中的变量名一致
m.put("age", "18"); //key名一定要与对象中的变量名一致
m.put("birthday", "1992-05-12"); //key名一定要与对象中的变量名一致
Student stu = new Student();
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.populate(stu, m); //将Map属性自动放到Bean中
System.out.println(stu.getName());
}
}