内省—beanutils工具包 操作javabean属性

内省—beanutils工具包 操作javabean属性 比 内省Introspector类操作javabean属性更加方便。内省—beanutils工具包 是Apache组织开发了一套用于操作JavaBean的API,Introspector是sun公司自己开发的一套用于操作JavaBean的API。
1. 新建工程,建包cn.itcast.beanutils,在包下建javabean类Student,代码如下:

package cn.itcast.beanutils;

    import java.util.Date;

    public class Student {

        private String name;
        private String password;
        private String email;
        private int age;        
        private Date birthday;


        public Date getBirthday() {
            return birthday;
        }

        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

}
  1. 在cn.itcast.beanutils下建Demo类操作Student类的各属性,代码如下:
    需注意导入commons-beanutils-1.9.2.jar和commons-logging-1.0.4.jar包
package cn.itcast.beanutils;

import java.lang.reflect.InvocationTargetException;
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;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

public class Demo {
    /*内省—beanutils工具包 
    Apache组织开发了一套用于操作JavaBean的API*/

    @Test
    public void test1() throws Exception, InvocationTargetException{
        Student stu=new Student();
        String name="maomao";
        String password="123";
        String email="123@qq.com";
        int age=23;
        //1. BeanUtils.setProperty设置Student stu的各属性
        BeanUtils.setProperty(stu, "name", name);
        BeanUtils.setProperty(stu, "password", password);
        BeanUtils.setProperty(stu, "email", email);
        BeanUtils.setProperty(stu, "age",age);

        //2.BeanUtils.getProperty获取Student stu的各属性
        String Name=BeanUtils.getProperty(stu, "name");
        String Password=BeanUtils.getProperty(stu, "password");
        String Email=BeanUtils.getProperty(stu, "email");
        String Age=BeanUtils.getProperty(stu, "age");
        System.out.println("name="+Name+", password="+password+", email="+Email+", age="+Age);
    }

    @Test
    public void test2() throws Exception, InvocationTargetException{
        Student stu=new Student();
        //当数据类型间不能直接转换时,需要调用转换器实现数据类型的转换
        String birthday="1999-09-09";
        //因为字符串不能转换成Date类型,所以此处需要注册一个转换器.下面代码表示遇到Date类型的数据时调用DateConverter()转换器
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        BeanUtils.setProperty(stu, "birthday",birthday);
        System.out.println(BeanUtils.getProperty(stu, "birthday"));
    }

    @Test
    public void test3() throws Exception, InvocationTargetException{
        Student stu=new Student();
        //当数据类型间不能直接转换时,需要调用转换器实现数据类型的转换,自己编写转换器
        String birthday="1999-09-09";
        //下面代码表示自己编写一个实现字符串到日期类型的转换器
        //注册转换器,    //Converter是接口类型,不能直接new 接口,new接口时必须实现接口的抽象方法
        ConvertUtils.register(new Converter(){
            public Object convert(Class type, Object value) { 
                if(value==null){
                    return null;
                }
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = format.parse((String)value);
                } catch (ParseException e) {
                    throw new ConversionException(e);
                }
                return date;
            }       
        }, Date.class);
        BeanUtils.setProperty(stu, "birthday",birthday);
        System.out.println(BeanUtils.getProperty(stu, "birthday"));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值