内省与BeanUtils的使用
for(Pro属性:javaBean中的getXXX,setXXX称为一个属性(少了其中的一个,也是一个属性) 问题:如何判断一个javaBean有多少属性? 不光是看这一个类有多少看get,set还有看父类,加一起才为它有多少属性 1.运用内省: public void test1() throws Exception{ //进行内省,了解其所有属性、公开的方法和事件(描述目标 bean 的 BeanInfo 对象) BeanInfo info=Introspector.getBeanInfo(Person.class); //得到属性描述器 PropertyDescriptor[] pds=info.getPropertyDescriptors(); for(PropertyDescriptor e:pds){ System.out.println(e.getName()); } } //不行获得父亲的属性 @Test public void test2() throws Exception{ //进行内省,了解其所有属性、公开的方法和事件(描述目标 bean 的 BeanInfo 对象) BeanInfo info=Introspector.getBeanInfo(Person.class,Object.class); //得到属性描述器 PropertyDescriptor[] pds=info.getPropertyDescriptors(); pertyDescriptor e:pds){ System.out.println(e.getName()); } } //操纵Bean的制定属性:name @Test public void test3() throws Exception{ Person p=new Person(); PropertyDescriptor psd=new PropertyDescriptor("name",Person.class); //得到属性的写方法,为属性赋值 Method method=psd.getWriteMethod();//setName method.invoke(p,"asdfg"); method=psd.getReadMethod(); method.invoke(p,null); } @Test public void test4() throws Exception{ Person p=new Person(); PropertyDescriptor psd=new PropertyDescriptor("name",Person.class); System.out.println(psd.getPropertyType()); //得到属性的写方法,为属性赋值 Method method=psd.getWriteMethod();//setName method.invoke(p,"asdfg"); //得到属性的读方法 method=psd.getReadMethod(); method.invoke(p,null); } } 2,利用BeanUtils: 方法:设置bean的属性:BeanUtils.setProperty(bean, name, value); 得到Bean的属性:BeanUtils.getProperty(bean,name);
还有两个很重要的方法:copyBean(,),将一个bean的属性copy到另一个属性上,但是注意的是:只copy属性名一样的
populate(),将map的value,填充到bean中,(map的key要与bean的属性名一样) public class Demo1 { @Test public void test(){ Person p=new Person(); try { BeanUtils.setProperty(p,"name","Luyi"); System.out.println(p.getName()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // @Test public void test2() throws IllegalAccessException, InvocationTargetException{ //由表单得到的数据 String name="a"; String password="b"; String ago="45"; String birthday=""; //注册一个日期转换器
ConvertUtils.register(new Converter(){ public Object convert(Class type, Object value) { if(value==null) return null; if(!(value instanceof String)){ //这样是给人看的,不提倡,上一层不知道 //System.out.println("不转"); //正确的做法 throw new ConversionException("只支持String转换"); } //通过了上边的两关说明是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); //ConvertUtils.convert(birthday,Date.class); Person p=new Person(); //只支持基本的八种类型 BeanUtils.setProperty(p,"name",name); BeanUtils.setProperty(p,"password",password); BeanUtils.setProperty(p,"ago",ago); BeanUtils.setProperty(p,"birthday",birthday); System.out.println(p.getName()) ; System.out.println(p.getPassword()) ; System.out.println(p.getAgo()); System.out.println(p.getBirthday()); } @Test public void test3() throws IllegalAccessException, InvocationTargetException{ //由表单得到的数据 String name="a"; String password="b"; String ago="45"; String birthday="1980-09-09"; ConvertUtils.register(new DateLocaleConverter(),Date.class); Person p=new Person(); //只支持基本的八种类型 BeanUtils.setProperty(p,"name",name); BeanUtils.setProperty(p,"password",password); BeanUtils.setProperty(p,"ago",ago); BeanUtils.setProperty(p,"birthday",birthday); System.out.println(p.getName()) ; System.out.println(p.getPassword()) ; System.out.println(p.getAgo()); Date date=p.getBirthday(); System.out.println(p.getBirthday().toLocaleString()); } @Test public void test4() throws IllegalAccessException, InvocationTargetException{ HashMap hm=new HashMap(); hm.put("name", "a"); hm.put("password","123"); hm.put("ago",12); hm.put("birthday","1998-9-9"); Person p=new Person();
//这个转换器有一个缺点:就是对空字符串无法处理 ConvertUtils.register(new DateLocaleConverter(),Date.class); //把Map中的集合填充到Bean的属性 //注意:map中的key要与属性一样 BeanUtils.populate(p,hm); System.out.println(p.getName()) ; System.out.println(p.getPassword()) ; System.out.println(p.getAgo()); Date date=p.getBirthday(); System.out.println(p.getBirthday().toLocaleString()); //Wed Sep 09 00:00:00 CST 1998 } }
现在的几乎什么都不懂,但是不代表以后的我也是这样。