使用Apache的BeanUtils工具类操作JavaBean属性

BeanUtils是Apache提供的操作JavaBean属性的工具类,比Java自带的Introspector类用起来方便很多;

但是需要引入commons-beanutils-1.9.2.jarcommons-logging-1.2.jar两个jar包。

BeanUtils操作JavaBean属性的实例:

Person类:

package com.test.beanutil;

import java.util.Date;

/**
 * Person
 * @author xuhu
 *
 */
public class Person
{
    //id
    private int id; 
    //姓名
    private String name;
    //年龄
    private int age;
    //生日
    private Date birthday;
    
    
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    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;
    }
    
    
}




BeanUtils方式操作Person类的属性:

package com.test.beanutil;

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.junit.Test;

public class BeanUtilTest
{
    @Test
    public void test1() throws Exception
    {
        Person p = new Person();
        
        String id = "1";
        String name = "test1";
        String age = "15";
        
        String birthday = "1999-06-06";
        
        //注册Date类的转换器
        ConvertUtils.register(new Converter()
        {
            @Override
            public <T> T convert(Class<T> arg0, Object arg1)
            {
                //判断传入的值
                if(arg1 == null)
                {
                    return null;
                }
                
                if(!(arg1 instanceof String))
                {
                    throw new ConversionException("仅支持String类型的转换");
                }
                
                String date = String.valueOf(arg1);
                if(date.trim().equals(""))
                {
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try
                {
                    return (T) sdf.parse(date);
                }
                catch (ParseException e)
                {
                    throw new ConversionException(e);
                }
            }
        }, Date.class);
        
        //设置属性
        BeanUtils.setProperty(p, "id", id);
        BeanUtils.setProperty(p, "name", name);
        BeanUtils.setProperty(p, "age", age);
        BeanUtils.setProperty(p, "birthday", birthday);
        
        //读取属性
        System.out.println(BeanUtils.getProperty(p, "id"));
        System.out.println(BeanUtils.getProperty(p, "name"));
        System.out.println(BeanUtils.getProperty(p, "age"));
        System.out.println(BeanUtils.getProperty(p, "birthday"));
        
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值