JavaBean是一种特殊的Java类,满足JavaBean API的规范,拥有getter/setter设置的属性。一般使用以下两种方式对JavaBean进行操作:
- 使用内省(IntroSpector)操作JavaBean
- 使用beanUtils框架(需要下载的jar:beanutils)
下面是两种方法的基本使用
- 使用内省操作JavaBean
package com.kexin.day1;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;
/*
* 使用内省(IntroSpector)操作JavaBean
*/
public class Demo4 {
@Test
public void test() throws Exception{
//使用IntroSpector获取JavaBean的属性描述器
BeanInfo info = Introspector.getBeanInfo(perso.class,Object.class);
PropertyDescriptor pd[] = info.getPropertyDescriptors();
for(PropertyDescriptor p:pd){
System.out.println(p.getName());
}
}
@Test
public void test1() throws Exception{
//使用内省设置JavaBean属性
perso p = new perso();
BeanInfo info = Introspector.getBeanInfo(perso.class,Object.class);
PropertyDescriptor pd = new PropertyDescriptor("name", perso.class);
Method m = pd.getWriteMethod();
m.invoke(p, "Tom");
System.out.println("I am "+p.getName());
}
@Test
public void test2() throws Exception{
//使用内省读取JavaBean属性
perso p = new perso();
BeanInfo info = Introspector.getBeanInfo(perso.class,Object.class);
PropertyDescriptor pd = new PropertyDescriptor("name", perso.class);
Method m = pd.getReadMethod();
String name = (String) m.invoke(p);
System.out.println("I am "+name);
}
}
class perso{
private String name = "Lisa";
private int age;
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;
}
}
- 使用beanUtils框架(不可用于内部类)
package com.kexin.day1;
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.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操作JavaBean
public class Demo5 {
@Test
public void test() throws IllegalAccessException, ReflectiveOperationException{
//使用BeanUtils操作JavaBean属性(8种基本数据类型)
Person1 p = new Person1();
String name = "Tom";
String age = "12";
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);//尽管字段age声明类型为int但是Beanutils可以自动在8种基本数据类型中进行转换
System.out.println(BeanUtils.getProperty(p, "name"));
System.out.println(p.getName()+" is "+p.getAge()+" years old");
}
@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
//使用BeanUtils操作JavaBean的非基本数据类型的数据,例如Date
Person1 p = new Person1();
String name = "Tom";
String age = "12";
String birthday = "2005-02-02";
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
//因为Date不是基本数据类型,所以需要注册一个转换器
ConvertUtils.register(new Converter() {
//将String转换为Date
@Override
public Object convert(Class type, Object value) {
if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只支持String类型");
}
String svalue = (String)value;
if(svalue.trim().equals("")){
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date;
try {
date = sdf.parse(svalue);
return date;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName()+" is "+p.getAge()+" years old and his birthday is "+p.getBirthday().toLocaleString());
}
@Test
public void test2() throws IllegalAccessException, InvocationTargetException{
//使用BeanUtils提供的转换器
Person1 p = new Person1();
String name = "Tom";
String age = "12";
String birthday = "2005-02-02";
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName()+" is "+p.getAge()+" years old and his birthday is "+p.getBirthday().toLocaleString());
}
@Test
public void test3() throws IllegalAccessException, InvocationTargetException{
//使用BeanUtils的populate方法将Map填充至bean
Map<String, String> map = new HashMap<>();
map.put("name", "Jerry");
map.put("age", "20");
map.put("birthday", "1997-02-02");
Person1 p = new Person1();
//依旧需要注册日期转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
BeanUtils.populate(p, map);
System.out.println(p.getName()+" is "+p.getAge()+" years old and his birthday is "+p.getBirthday().toLocaleString());
}
}
Person类:
package com.kexin.day1;
import java.util.Date;
public class Person1 {
private String name;
private int age;
private Date birthday;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}