内省—beanutils工具包
Apache组织开发了一套用于操作JavaBean的API,这套API考虑到了很多实际开发中的应用场景,因此在实际开发中很多程序员使用这套API操作JavaBean,以简化程序代码的编写。
Beanutils工具包的常用类:
•BeanUtils
•PropertyUtils
•ConvertUtils.regsiter(Converter convert, Class clazz)
•自定义转换器
package cn.itcast.beanutils;
import java.util.Date;
public class Person {
private String name;
private String password;
private int age;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(int age) {
this.age = age;
}
}
package cn.itcast.beanutils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.xml.crypto.Data;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;
//使用beanUtils操纵bean的属性 ( 第三方)
public class Demo1 {
@Test
public void test1() throws Exception {
Person p = new Person();
BeanUtils.setProperty(p, "age", 456);// ???哪个bean,哪个属性,哪个值。
System.out.println(p.getAge());// 456
}
@Test
public void test2() throws Exception {
String name = "aaaa";
String age = "123";
String password = "pw";
Person p = new Person();
// 支持8种基本类型自动转换
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);// beanutils默认只支持8种基本类型的转换
// 。beanutils支持把字符串转成int.
BeanUtils.setProperty(p, "password", password);
System.out.println(p.getName());// aaaa
System.out.println(p.getAge());// 123
System.out.println(p.getPassword());// pw
}
@Test
public void test3() throws Exception {
// String birthday = "1983-12-1";
String birthday = "";
// 为了让日期赋值到bean的birthday属性上,需要给beanUtils注册一个日期转换器
// ConvertUtils.register(converter, clazz);
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
if (value == null)
return null;
if (!(value instanceof String)) {
throw new ConversionException("只支持String类型的转换");
}
String str = (String) value;
if (str.trim().equals(""))
return null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);// 接受字符串,并且转成日期返回。
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
Person p = new Person();
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getBirthday());
}
// 使用apache的转换器(有bug)
@Test
public void test4() throws Exception {
String birthday = "1983-12-1";
// String birthday = "";
ConvertUtils.register(new DateLocaleConverter(), Data.class);
Person p = new Person();
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getBirthday());
}
// 用map集合填充bean属性
@Test
public void test5() throws Exception {
Map map = new HashMap();
map.put("name", "aaa");
map.put("password", "123");
map.put("birthday", "1980-09-09");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person bean = new Person();
// 用map集合填充bean属性,map关键字和bean属性要一致
BeanUtils.populate(bean, map); // populate 填充,将map集合填充到bean里面
System.out.println(bean.getName());// aaaa
System.out.println(bean.getPassword());// 123
System.out.println(bean.getBirthday());// Tue Sep 09 00:00:00 CST 1980
}
}