A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© pzfpang449 中级黑马   /  2013-1-23 17:42  /  1968 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

操作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());
               
        }
       
}




评分

参与人数 1黑马币 +5 收起 理由
李培根 + 5 感谢分享

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
单纯的分享是没有技术分的,只有金币奖励。建议楼主还是多多提问,以及回答别人问题,现在技术分已经很好得了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马