Java内省技术
一、利用内省API操作JavaBean的属性
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
/**
* 利用内省API操作JavaBean的属性
* @author WangYifeng
*
* 2018年9月22日下午10:15:32
*/
public class Demo1 {
public static void main(String[] args) throws Exception {
getProperty();
System.out.println("-------------华丽的分割线------------");
getProperty1();
System.out.println("-------------华丽的分割线------------");
getProperty2();
}
//利用内省获取JavaBean的全部属性
public static void getProperty() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : propertyDescriptors) {
System.out.print(pd.getName()+",");
}
System.out.println();
}
//利用内省获取JavaBean的自身属性,不包含从父类继承的属性
public static void getProperty1() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor pd : propertyDescriptors) {
System.out.print(pd.getName()+",");
}
System.out.println();
}
//操作JavaBean的指定属性
public static void getProperty2() throws Exception {
Person p = new Person();
//创建JavaBean指定属性的属性描述器PropertyDescriptor
PropertyDescriptor pd = new PropertyDescriptor("name", Person.class);
//根据属性描述器获取属性的类型
System.out.println(pd.getPropertyType());
//根据属性描述器获取属性的读方法读取属性的值,即getter方法
Method readMethod = pd.getReadMethod();
System.out.println(readMethod.invoke(p, null));
//根据属性描述器获取属性的写方法修改属性的值,即setter方法
Method writeMethod = pd.getWriteMethod();
writeMethod.invoke(p, "李四");
System.out.println(readMethod.invoke(p, null));
}
}
运行结果:
二、利用BeanUtils操作JavaBean的属性
下载第三方开源库的jar包,导入当前project中
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
/**
* 利用BeanUtils操作JavaBean的属性
* @author WangYifeng
*
* 2018年9月22日下午10:15:32
*/
public class Demo2 {
public static void main(String[] args) throws Exception {
useBeanUtils();
useBeanUtils1();
}
//利用beanUtils操作JavaBean的指定属性
public static void useBeanUtils() throws Exception {
Person p = new Person();
//操作属性的值
BeanUtils.setProperty(p, "name", "王五"); //只能操作8中基本数据类型
//获取属性的值
System.out.println(BeanUtils.getProperty(p, "name"));
}
//利用beanUtils操作JavaBean的指定属性
public static void useBeanUtils1() throws Exception {
Person p = new Person();
//为了将日期赋到birthday属性上,我们需要给BeanUtils注册一个日期转换器
ConvertUtils.register(new Converter() {
@Override
public <T> T convert(Class<T> arg0, Object arg1) {
if(arg1==null) {
return null;
}
if(arg1.getClass() != String.class) {
throw new ConversionException("只支持String类型转换。");
}
String str = (String) arg1;
if(str.trim().equals("")) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return (T) sdf.parse(str);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
//操作属性的值
BeanUtils.setProperty(p, "birthday", "1995-11-23"); //只能操作8中基本数据类型
//获取属性的值
// String property = BeanUtils.getProperty(p, "birthday");
Date birthday = p.getBirthday();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:SS");
System.out.println(sdf.format(birthday));
}
}
注意:BeanUtils的setProperty()方法只支持对基本数据类型的操作,如需要操作复杂类型,则需要为BeanUtils注册一个类型转换器ConvertUtils.register();
运行结果: