Java——JavaBean基础

JavaBean是一种特殊的Java类,满足JavaBean API的规范,拥有getter/setter设置的属性。一般使用以下两种方式对JavaBean进行操作:
- 使用内省(IntroSpector)操作JavaBean
- 使用beanUtils框架(需要下载的jar:beanutils
下面是两种方法的基本使用
- 使用内省操作JavaBean

package com.kexin.day1;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import org.junit.Test;
/*
 * 使用内省(IntroSpector)操作JavaBean
 */
public class Demo4 {
    @Test
    public void test() throws Exception{
        //使用IntroSpector获取JavaBean的属性描述器
        BeanInfo info = Introspector.getBeanInfo(perso.class,Object.class);
        PropertyDescriptor pd[] = info.getPropertyDescriptors();
        for(PropertyDescriptor p:pd){
            System.out.println(p.getName());
        }
    }
    @Test
    public void test1() throws Exception{
        //使用内省设置JavaBean属性
        perso p = new perso();
        BeanInfo info = Introspector.getBeanInfo(perso.class,Object.class);
        PropertyDescriptor pd = new PropertyDescriptor("name", perso.class);
        Method m = pd.getWriteMethod();
        m.invoke(p, "Tom");
        System.out.println("I am "+p.getName());
    }
    @Test
    public void test2() throws Exception{
        //使用内省读取JavaBean属性
        perso p = new perso();
        BeanInfo info = Introspector.getBeanInfo(perso.class,Object.class);
        PropertyDescriptor pd = new PropertyDescriptor("name", perso.class);
        Method m = pd.getReadMethod();
        String name = (String) m.invoke(p);
        System.out.println("I am "+name);
    }
}
class perso{
    private String name = "Lisa";
    private int age;
    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;
    }

}
  • 使用beanUtils框架(不可用于内部类
package com.kexin.day1;

import java.lang.reflect.InvocationTargetException;
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操作JavaBean
public class Demo5 {
    @Test
    public void test() throws IllegalAccessException, ReflectiveOperationException{
        //使用BeanUtils操作JavaBean属性(8种基本数据类型)
        Person1 p = new Person1();
        String name = "Tom";
        String age = "12";
        BeanUtils.setProperty(p, "name", name);
        BeanUtils.setProperty(p, "age", age);//尽管字段age声明类型为int但是Beanutils可以自动在8种基本数据类型中进行转换
        System.out.println(BeanUtils.getProperty(p, "name"));
        System.out.println(p.getName()+" is "+p.getAge()+" years old");
    }
    @Test
    public void test1() throws IllegalAccessException, InvocationTargetException{
        //使用BeanUtils操作JavaBean的非基本数据类型的数据,例如Date
        Person1 p = new Person1();
        String name = "Tom";
        String age = "12";
        String birthday = "2005-02-02";
        BeanUtils.setProperty(p, "name", name);
        BeanUtils.setProperty(p, "age", age);
        //因为Date不是基本数据类型,所以需要注册一个转换器
        ConvertUtils.register(new Converter() {
            //将String转换为Date
            @Override
            public Object convert(Class type, Object value) {
                if(value==null){
                    return null;
                }
                if(!(value instanceof String)){
                    throw new ConversionException("只支持String类型");
                }
                String svalue = (String)value;
                if(svalue.trim().equals("")){
                    return null;
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date;
                try {
                    date = sdf.parse(svalue);
                    return date;
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }, Date.class);
        BeanUtils.setProperty(p, "birthday", birthday);
        System.out.println(p.getName()+" is "+p.getAge()+" years old and his birthday is "+p.getBirthday().toLocaleString());
    }
    @Test
    public void test2() throws IllegalAccessException, InvocationTargetException{
        //使用BeanUtils提供的转换器
        Person1 p = new Person1();
        String name = "Tom";
        String age = "12";
        String birthday = "2005-02-02";
        BeanUtils.setProperty(p, "name", name);
        BeanUtils.setProperty(p, "age", age);
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        BeanUtils.setProperty(p, "birthday", birthday);
        System.out.println(p.getName()+" is "+p.getAge()+" years old and his birthday is "+p.getBirthday().toLocaleString());
    }
    @Test
    public void test3() throws IllegalAccessException, InvocationTargetException{
        //使用BeanUtils的populate方法将Map填充至bean
        Map<String, String> map = new HashMap<>();
        map.put("name", "Jerry");
        map.put("age", "20");
        map.put("birthday", "1997-02-02");
        Person1 p = new Person1();
        //依旧需要注册日期转换器
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        BeanUtils.populate(p, map);
        System.out.println(p.getName()+" is "+p.getAge()+" years old and his birthday is "+p.getBirthday().toLocaleString());
    }
}

Person类:

package com.kexin.day1;

import java.util.Date;

public class Person1 {
    private String name;
    private int age;
    private Date birthday;
    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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄鸭and小黑鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值