这篇博客是对JAVA内省JavaBean(Introspector、BeanInfo和PropertyDescriptor)的补充,里面提到了BeanUtils工具包中BeanUtils、PropertyUtils和ConverUtils的使用。记住要导入两个包。
BeanUtils:以字符串的形式对javabean类的属性进行操作;
PropertyUtils:以属性本身的类型进行操作
ConverUtils:主要用到ConverUtils.regsiter,这个方法主要是转换非基本数据类型用的
三者的使用具体看主要看代码注释分析(主要结合我的那篇博客看):
先写个Person类:
public class Person {
/*
* 小知识点:请问Person类中有几个属性
* 有5个:分别是name、age、password、AA和class(Object中的getClass方法)
* 一个自定义的类有几个属性不是由字段决定的,而是由getXXX()或setXXX()决定的
* */
private String name;//字段
private int age;//字段
private int password;//字段
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAA()
{
return null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
}
下面是对Person类的相关操作(用到了Junit测试):
public class TestPerson {
//以前传统的做法:得到Person中bean的所有属性
@Test
public void test1()throws Exception
{
BeanInfo info=Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] ps=info.getPropertyDescriptors();
System.out.println(ps.length);
for(PropertyDescriptor p:ps)
{
//从打印的结果就知道,Person类中有5个属性
System.out.println(p.getName());
}
}
//操作bean特定属性:age
@Test
public void test2()throws Exception
{
PropertyDescriptor p=new PropertyDescriptor("age", Person.class);
Method setMethod=p.getWriteMethod();
Method getMethod=p.getReadMethod();
Person pp=new Person();
setMethod.invoke(pp, 23);
System.out.println(getMethod.invoke(pp, null));
}
@Test
public void test3()throws Exception
{
//BeanUtils:是以字符串形式对javabean进行操作,
Person p=new Person();
//它会自动的吧字符串形式的数组转换成int类型(只要是基本数据类型都可以)
BeanUtils.setProperty(p, "age", "23");
System.out.println("BeanUtils::"+p.getAge());
//PropertyUtils:是以属性本身的类型进行操作
PropertyUtils.setProperty(p, "age", 12);
System.out.println("PropertyUtils::"+p.getAge());
}
@Test
public void test4()throws Exception
{
String name="读卡机";
int age=55;
int password=1233121;
Person p=new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
}
@Test
public void test5()throws Exception
{
String name="读卡机";
int age=55;
int password=1233121;
String birthday="2012-3-12";
//为了让Date类型的birthday能够赋值,我们需要给BeanUtils注册一个Date的转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class bean, Object value) {
if(value==null)
{
return null;
}
if(!(value instanceof String))
{
throw new ConversionException("不支持的格式");
}
String valueStr=(String) value;
if(valueStr.trim().equals(""))
{
return null;
}
SimpleDateFormat forma=new SimpleDateFormat("yyyy-MM-dd");
//注意:这里只能try catch,因为接口Converter的convert方法没有抛异常,老子不抛,儿子怎么敢抛呢
try {
Date d=forma.parse(valueStr);
return d;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
Person p=new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
//如果没有ConvertUtils.register会报错,因为birthday的属性是Date类型的,
//不是基本数据类型,看test3怎么说的
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBirthday());
}
@Test
public void test6()throws Exception
{
String name="读卡机";
int age=55;
int password=1233121;
String birthday="2012-3-12";
//优化test5
/*
为了让Date类型的birthday能够赋值,我们需要给BeanUtils注册一个Date的转换器,下面这个转换器是
beanutils工具包中写好的,我们可以直接使用,不过这个方法有bug
举个例子:假如String birthday="";而没有实际的值,那么下面这个方法就会报错,而test5就不会
因为我们在内部自己定义了方法,进行判断了。
* */
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p=new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "password", password);
//如果没有ConvertUtils.register会报错,因为birthday的属性是Date类型的,
//不是基本数据类型,看test3怎么说的
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBirthday());
}
}