内省是 Java 语言对 Bean 类属性、事件的一种处理方法(也就是说给定一个javabean对象,我们就可以得到/调用它的所有的get/set方法)。
例如类 A 中有属性 name, 那我们可以通过 getName,setName 来得到其值或者设置新的值。通过 getName/setName 来访问 name 属性,这就是默认的规则。
Java 中提供了一套 API 用来访问某个属性的 getter/setter 方法,通过这些 API 可以使你不需要了解这个规则,这些 API 存放于包 java.beans 中。
一般的做法是通过类 Introspector 的 getBeanInfo方法 来获取某个对象的 BeanInfo 信息,然后通过 BeanInfo 来获取属性的描述器(PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的 getter/setter 方法,然后我们就可以通过反射机制来调用这些方法。
首先建立一个bean
package com.fts.beanutils;
import java.util.Date;
public class Person {
private String name;
private int passwd;
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 void setName(String name) {
this.name = name;
}
public int getPasswd() {
return passwd;
}
public void setPasswd(int passwd) {
this.passwd = passwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
使用beanutils操作bean
package com.fts.beanutils;
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.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, "name", "fengtansong");
System.out.println(p.getName());
}
@Test
public void test2() throws Exception{
String name="fts";
String passwd="888888";
String age="100";
Person p=new Person();
//数据转换只支持八种基本数据类型
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "passwd", passwd);
BeanUtils.setProperty(p, "age", age);
System.out.println(p.getName());
System.out.println(p.getPasswd());
System.out.println(p.getAge());
}
@Test
public void test3() throws Exception{
String name="fts";
String passwd="888888";
String age="100";
String birthday="1992-02-28";
Person p=new Person();
//ConvertUtils.register(new DateLocaleConverter(),Date.class);
ConvertUtils.register(new Converter(){
public Object convert(Class type, Object value) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}, Date.class);
//数据转换只支持八种基本数据类型
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "passwd", passwd);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPasswd());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
@Test
public void test4() throws Exception{
Map map=new HashMap();
map.put("name","kkk");
map.put("passwd","666666");
map.put("age","100");
map.put("birthday", "1992-02-28");
Person bean=new Person();
ConvertUtils.register(new DateLocaleConverter(),Date.class);
BeanUtils.populate(bean, map);
System.out.println(bean.getName());
System.out.println(bean.getPasswd());
System.out.println(bean.getAge());
System.out.println(bean.getBirthday());
}
}
用junit测试的结果如下
fengtansong
fts
888888
100
fts
888888
100
Fri Feb 28 00:00:00 GMT+08:00 1992
kkk
666666
100
Fri Feb 28 00:00:00 GMT+08:00 1992