操作JavaBean
1,getBeaninfo()获得BeanInfo.获得bean的所有属性
2,getPropertyDescriptors()获得属性的属性描述器
3,getReadMethod()获得属性的些方法
package com.itcast.introspector;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;
//使用内省api操作bean属性
public class neixingapi {
//得到Bean的所有属性
@Test
public void test() throws IntrospectionException{
BeanInfo info=Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds= info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getName());
}
}
//操作Bean的指定属性
@Test
public void test2() throws Exception{
Person p=new Person();
PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);
//获得set属性public void setAge(int age) {
Method method=pd.getWriteMethod();
method.invoke(p, 45);
//获得get属性public int getAge() {
method=pd.getReadMethod();
System.out.println(method.invoke(p,null));
}
//获得属性的属性
@Test
public void test3()throws Exception{
Person p=new Person();
PropertyDescriptor pd=new PropertyDescriptor("age", Person.class);
System.out.println(pd.getPropertyType());
}
}
2,BeanUtils
1)使用Beanutils操作bean属性
BeanUtils.setProperty(p, "name", "aaa");
System.out.println(p.getName());
2)自定义转换器(例如日期)
ConvertUtils.register(new Converter(){},Date.class);
3)Apache自带的日期转换
ConvertUtils.register(new DateLocaleConverter(), Date.class);
4)将map数据放到bean中 用BeanUtils.populate()填充bean的属性
BeanUtils的例子如下
package com.itcast.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 beanutilneixing {
@Test
public void test() throws Exception{
Person p=new Person();
BeanUtils.setProperty(p, "name", "aaa");
System.out.println(p.getName());
}
//自定义转换,下面代码有问题,因为beanutils框架只支持基本数据类型转换
@Test
public void test2()throws Exception{
String name="aa";
String password="123";
String age="11";
String birthday="1998-02-20";
ConvertUtils.register(new Converter() {
@Override
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 sf=new SimpleDateFormat("yyyy-MM-dd");
try {
return sf.parse(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);//异常不能断
}
}
},Date.class);
Person p=new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
//Apache带的日期转换方法,有bug如果birthday为""时报错
@Test
public void test3()throws Exception{
String name="aa";
String password="123";
String age="11";
String birthday="1998-02-20";
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p=new Person();
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "password", password);
BeanUtils.setProperty(p, "age", age);
BeanUtils.setProperty(p, "birthday", birthday);
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBirthday());
}
//将map数据放到bean中 用BeanUtils.populate()填充bean的属性
@Test
public void test4()throws Exception{
Map map=new HashMap();
map.put("name", "aa");
map.put("password", "123");
map.put("age", "23");
map.put("birthday", "1999-9-9");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person bean=new Person();
//用map集合中的值,田中bean的属性
BeanUtils.populate(bean, map);
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getAge());
System.out.println(bean.getBirthday());
}
}
|